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

Posts: 374
Registered: ‎02-12-2011
My Device: Bold 9780
My Carrier: Wind Mobile
Accepted Solution

SimpleDateFormat formatting hour question

[ Edited ]
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
final DateField timeField = new DateField("", 0, timeFormat);

 Heres how I am trying to format, then I'm filling timeField with my 




I am triyng to show the time between my events, it all formats fine (minutes and seconds) but the hour field of the formatted string always shows the current our of the day and not hour between my events!?!


It should always start at 0 and then increase every hour. I dont want it to show the hour of the day, but the hour of my own timer Smiley Frustrated


I find it weird that all the other fields work fine, not showing the minute or second in the day but the hour field won't give.

Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: SimpleDateFormat formatting hour question

to display the date is the functionality of the datefield.
check what date/time your splitTime actually represents, bb os uses milliseconds since the epoch.
That means if you set the datefield to 1000 (ms) it means you set it to 1000ms after 00:00:00 UTC on 1 January 1970 - probably not what you want.

it may be possible to repurpose the datefield for your task, but most likely it would be easier to use your own objectchoicefields.
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Posts: 137
Registered: ‎09-30-2009
My Device: Torch 9800

Re: SimpleDateFormat formatting hour question

Yes simon_hain is right here. You should go with objectchoicefield which you can customize for your current needs in fastest possible time.
Press like if you find a message helpful
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: SimpleDateFormat formatting hour question

I use DateField for this purpose.  Hours and minutes display correctly. 


What you have not considered is that the DateField is TimeZone specific and by default, uses the local TimeZone.  So, say you give it a time of 1 hour, then this actually means 1 hour past midnight on the Epoch.  Now the Epoch is UTC, so this time will then get converted to what every 01:00 on the 1 Jan 1970 would be in your TimeZone.  That is the problem you are having.


To overcome this problem, is simple,  Just set the TimeZone for the DateField to be GMT.  And this should resolve your problem. 


If you are using this as an input Field, then you really have to be aware that it actually displaying a date - just not showing the Date part.  So when you do a getTime() from the Field you have to extract just the hour and minute and ignore the date part.  For example, say you display 18:00 because the user has been doing something for 18 hours.  The user changes this to 1:00 and being sensible, rolls the control up to 19, 20, 21, etc to 1.  Then the DateField will give you back the next day (i.e. 25 hours) not 1 hour.  Same happens if the user rolls the time back. 


So the trick is set the TimeZone on the DateField to GMT, and make sure you factor out any Date component if you are using this for input. 


Hope this is clear.