Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800
My Carrier: AT&T
Accepted Solution

Date calculation problems

[ Edited ]

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().getTime(), c.getTime().getTime());
}

 

 

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Date calculation problems

[ Edited ]

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().getTime(), c.getTime().getTime());
}

 

 

 

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Date calculation problems

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.

Developer
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: Date calculation problems

Good catch, didn't even consider DST!

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Date calculation problems

Only a good catch because I have been caught out by it!  Smiley Sad

Developer
Posts: 79
Registered: ‎11-10-2009
My Device: Torch 9800
My Carrier: AT&T

Re: Date calculation problems

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().getTime(), c.getTime().getTime());
}

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Date calculation problems

"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.