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: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: String Date Between Dates

You need to think hard about what times are, and which times are invariant and which times should change when you change time zone. 

 

If these are invariant times, then the easiest way to process them is to create a local time Calendar object, then substring the date and set the Month, year, day yourself (and clear the time).  Then when you display this, you will see local time. 

 

Alternatively, create GMT Calendar, set the times, then display these using a DateField that also uses a GMT Time Zone.

 

And since these came from a Server, you may need to be aware of the time that it thinks it is.

Contributor
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Thanks Peter.  I will just be comparing dates and just need to ensure the correct timezone (ofset) is used when httpdateparse.  I can ge the -4 (or -5 etc. depending on timezone on device) and just need to fiormat is as hr (eg "-04:00") so I can append to my date.  Is this something that can be done efficiently? I'm in the process of researching dates/times/zones now.  Trying to get this all sorted out.

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

Re: String Date Between Dates

Why bother - if you are just going to compare, then the time zone is not relevant?

Contributor
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

[ Edited ]

Looks like I might have it.  May be overkill, not sure.  This seems to work to return the correct date.

 

//break down string date and pass in params
TimeZone clientZone = TimeZone.getDefault();
Calendar cal = Calendar.getInstance(clientZone);
cal.set(Calendar.DAY_OF_MONTH, 5);
cal.set(Calendar.MONTH, 11 - 1);
cal.set(Calendar.YEAR, 2012);
  
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date(cal.getTime().getTime()));

Dialog.alert(date);

 

 Doesnt seem like clientZone is required.  Is this the correct approach to setting a calendar object from a string?

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

Re: String Date Between Dates

Code is exactly what I would do, except I would probably use

<SimpleDateFormat>.formatLocal(<long>)

 

But remember if the user changes time zone between the cal.set... and the String date, then the time will be wrong. 

Contributor
Posts: 32
Registered: ‎10-21-2012
My Device: 9900
My Carrier: NA

Re: String Date Between Dates

Thank you Peter.  Time wont change between cal sets.  These dates are figured out dynamically, used, the disposed of.