01-07-2011 04:35 PM - edited 01-07-2011 04:36 PM
I have a java (and much further back vb) background and it seems I'm really dependent on the built-in functions for date calculations. I'm trying to do a few things, but haven't been able to accomplish them.
I don't have
Calendar.add()
I briefly contemplated an add function by converting to long then doing the math and returning a new calendar object, but the problems with that approach just blossomed as soon as I started. Just trying to find out if a date was yesterday has me banging my head against the wall.
Any help please?
I wrote this, but I don't know if there's an easier method, going about it the wrong way, etc.
public static boolean isYesterday(Calendar c) {
Calendar today = Calendar.getInstance();
int newDay = today.get(Calendar.DAY_OF_MONTH);
if (newDay == 1) { //get last day of previous month
int newMonth = today.get(Calendar.MONTH);
int newYear = today.get(Calendar.YEAR);
/* If jan 1, get dec of last year */
if (newMonth == 0) { //Java Calendar.MONTH is zero-based
newMonth = 11;
newYear -= 1;
today.set(Calendar.YEAR, newYear);
}
today.set(Calendar.MONTH, newMonth);
newDay = DateTimeUtilities.getNumberOfDaysInMonth(newMonth, newYear);
} else {
newDay -= 1;
}
today.set(Calendar.DAY_OF_MONTH, newDay);
return DateTimeUtilities.isSameDate(today.getTime().getTi me(), c.getTime().getTime());
}
Solved! Go to Solution.
01-07-2011 05:04 PM - edited 01-07-2011 05:04 PM
I would probably just do something like:
public static boolean isYesterday(Calendar c) {
Calendar yesterday = Calendar.getInstance();
yesterday.setTimeInMillis(System.currentTimeMillis () - DateTimeUtilities.ONEDAY);
return DateTimeUtilities.isSameDate(yesterday.getTime().g etTime(), c.getTime().getTime());
}
01-07-2011 05:15 PM
Unfortunately just subtracting 1 day in milliseconds does not work around daylight saving time changes.
I don't think there is a simple way to subtract 1 day. If you are not interested in the time, then you could use DateTimeutilities to zero the time, then subtract 1 hour, then 0 the time again and you will have the correct date.
01-07-2011 05:22 PM
Good catch, didn't even consider DST!
01-07-2011 05:45 PM
Only a good catch because I have been caught out by it! ![]()
01-11-2011 10:59 AM
Yes, DST has been a bit of a problem when trying to get these things going. My date problems don't end here. I'm receiving an XML and parsing the date myself (no reg ex or parse!) and convert to the device's tz. I then need to compare it to the device's current time, find out when things happened, will happen, etc. And I seem to be missing Calendar.getTimeInMillis() though the JDE 6.0.0 docs say it should be there. Fine since it's the same as Calendar.getTime().getTime(); Are there any good third party libraries that can help with this so I don't spend the next bit reinventing the wheel and probably making it square in the process?
So, possibly:
public static boolean isYesterday2(Calendar c) {
Calendar today = Calendar.getInstance();
DateTimeUtilities.zeroCalendarTime(today);
today.setTime(new Date(today.getTime().getTime() - DateTimeUtilities.ONEHOUR));
return DateTimeUtilities.isSameDate(today.getTime().getTi me(), c.getTime().getTime());
}
01-11-2011 04:30 PM
"convert to the device's tz"
I recommend that you stick with using UTC for everything. If you need to display this to the user, then a DateField will convert the UTC time to the local time. SImpleDateFormat.formatLocal will also print out you UTC long time, in local time. So I would recommend that you do not convert any times to local.
Remember that System.currentTimeMillis() is UTC/GMT time.
Of course, whether this is good advice or not does depend on your application.
I am not aware of any third party code that helps, I've reinvented square wheels for all my date processing. But I never really looked.
Regarding your code, assuming that Calendar c is using the local TimeZone, then I think the code will work.