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
brendanmc25
Posts: 63
Registered: ‎08-07-2009
My Device: Not Specified
Accepted Solution

Compare 2 datetime Strings

Hi all,

 

I would really appreciate a pointer in the right direction when it comes to getting this bit of code to add 20 mins to lastEventDateString and then i need to  compare to see which is the most recent or not most recent

 

p.s I know i'm nowhere near, i have just been scanning google!

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); eventDateString = dateFormat.format(calendar.getTime()); lastEventDateString = dateFormat.format(splitSession[0]); //*** I need to add 20 mins here to lastEventDateString and see if it is more than eventDateString if(eventDateString > lastEventDateString) {

 

Developer
Posts: 43
Registered: ‎09-28-2009
My Device: Not Specified

Re: Compare 2 datetime Strings

Hi brendanmc25,

 

I would opt for using the inherent milisecond operations on the Date object, as long comparisons are considerably easier than String comparisons, also as well as considerably more efficient. If you are willing to go this route then you can do the following:

 

//get the current time long currentTimeInMilliseconds = Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime().getTime(); //add 20 minutes to the current time (time is in miliseconds, so 20 * 60 * 1000) currentTimeInMilliseconds += (20*60*1000) ; //get the date to compare, i assumed that splitSession[0] is a date object. int compareTimeInMilliseconds = splitSession[0].getTime(); //compare the two if (compareTimeInMilliseconds > currentTimeInMilliseconds) { //do stuff here

 

Hope this helps,

 

Regards

 

Andre

Developer
brendanmc25
Posts: 63
Registered: ‎08-07-2009
My Device: Not Specified

Re: Compare 2 datetime Strings

This looks great - thanks very much :smileyhappy:
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Compare 2 datetime Strings

Possibly more efficient code (doesn't need a Calendar Object), and doesn't have any problems with long to int conversions.  Also, I like using DateTimeUtilities for time constants.  Just posted for "completeness" really.  Not tested mind you, but you get the idea.

And yes, System.currentTimeMillis() is UTC time on the device.

//get the current time long currentTimeInMilliseconds = System.currentTimeMillis(); // Use DateTimeUtilitie constants to increment time currentTimeInMilliseconds += (long) (20 * DateTimeUtilities.ONEMINUTE); //get the date to compare, i assumed that splitSession[0] is a date object. long compareTimeInMilliseconds = splitSession[0].getTime(); //compare the two if (compareTimeInMilliseconds > currentTimeInMilliseconds) { //do stuff here