11-05-2012 02:39 PM
Hello. I've come up with a method that seems to work fine. Just thought there might be a more effective way of determining if a string date (must be from a string) is between 2 other dates (from string).
TimeZone clientZone = TimeZone.getDefault();
String dateToCompare="2012-10-03";
String dateBegin="2012-10-01";
String dateEnd="2012-10-07";
Date date1= new Date(HttpDateParser.parse(dateToCompare));
Date date2= new Date(HttpDateParser.parse(dateBegin));
Date date3= new Date(HttpDateParser.parse(dateEnd));
Calendar calToCompare = Calendar.getInstance(clientZone);
Calendar calBegin = Calendar.getInstance(clientZone);
Calendar calEnd = Calendar.getInstance(clientZone);
calToCompare.setTime(date1);
calBegin.setTime(date2);
calEnd.setTime(date3);
if(calToCompare.after(calBegin) && calToCompare.before(calEnd)) {
Dialog.alert("date is between");
}
else {
Dialog.alert("date is NOT between");
}
Is there a more effective way to accomplish this? Thank you.
Solved! Go to Solution.
11-05-2012 06:36 PM
Date date1= new Date(HttpDateParser.parse(dateToCompare));
Date date2= new Date(HttpDateParser.parse(dateBegin));
Date date3= new Date(HttpDateParser.parse(dateEnd));
long compareDateLong = date1.getTime();
long beginDateLong = date2.getTime();
long endTimeLong = date3.getTime();
if ( beginDateLong < compareDateLong ) && ( endTimeLong > compareDateLong ) {
Dialog.alert("date is between");
}
else {
Dialog.alert("date is NOT between");
}
11-05-2012 06:42 PM
Thank you, Peter.
11-06-2012 08:35 AM
Wondering if you could offer me some assistance or pointers. When I use the following code
strTheDate = new Date(HttpDateParser.parse("2012-11-09"));
Dialog.alert(strTheDate.toString());
The dialog box displays the 8th of Nov, not the 9th.
11-06-2012 08:53 AM
I suspect a Time Zone issue, but I don't use HttpDateparser so have never looked at how it processes when there is not a Time Zone specified. Review that and/or try with a Time Zone specified to see if you see some effect.
11-06-2012 09:20 AM
Thanks. I did try with a time and time zone but still same issue.
11-06-2012 09:47 AM
From the testing I have just done, the string:
2012-11-09
is treated midnight on the date specified, if you were on the GMT line. But when you do this:
strTheDate.toString()
the time is converted to local time. I suspect this explains the difference.
Check the time zone specified on the device/simulator that you are using.
11-06-2012 10:05 AM
Thanks time zone on device/simulator is -4 (Santiago). Do i need to somehow specify this when parsing?
11-06-2012 10:25 AM
Seems that when I specify date as "2012-11-09T00:00-04:00" that I get the correct date. Just need to determine now how to get string value of -04:00 from device.
11-06-2012 11:00 AM
Ok. I can get the raw offset using
TimeZone clientZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(clientZone);
Dialog.alert(Integer.toString(clientZone.getRawOff
Which will return -5. I need to somehow now format that as hour. eg. -05:00
Is there a way to properly do this?