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
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified
Accepted Solution

How to get the current time in GMT?

I've seen countless examples online, but I can't figure out why this isn't working:

 

 

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String eventDateString; Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); // GMT will always be supported by getTimeZone eventDateString = dateFormat.format(calendar.getTime()); System.out.println(eventDateString); Calendar calendarLocal = Calendar.getInstance(); eventDateString = dateFormat.format(calendarLocal.getTime()); System.out.println(eventDateString);

 

This results in this output:

 

2009-03-10 19:58:06
2009-03-10 19:58:06

 

I would expect the first one to be 4 hours ahead since the device I'm using is EST.

 

Am I missing something about how these methods work?

 

Thanks.

 

 

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

Re: How to get the current time in GMT?

In the API documentation for SimpleDateFormat, it says the following:

 "public class SimpleDateFormat
extends DateFormat

Formats and parses dates in a locale-sensitive manner"

 

Add the following to your code to demonstrate that the hours are in fact different in the Calendar objects:

 System.out.println(calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendarLocal.get(Calendar.HOUR_OF_DAY));

 

If you wish to use SimpleDateFormat to provide GMT output, you have to manipulate the time yourself, by adding the appropriate offset.

Developer
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Re: How to get the current time in GMT?

Thanks peter, I never thought it would be a problem with the formatter, since I thought it's only job was to format dates.

 

So SimpleDateFormat basically ignores the timezone of the Calendar that you give it and uses your local timezone?

 

Is there no formatter which takes a timezone?

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

Re: How to get the current time in GMT?

"Is there no formatter which takes a timezone?"

 

Think that DateFormat does, though I have never used it.  The java doc says:

public abstract class DateFormat
extends Format

Defines fundamental functionality for locale-independent formatting of date/time information.

 

Have a look at it and if you get it to work for you, I'd be interested to see some sample code.

 

DateFields respect TimeZone too.

Developer
Developer
Posts: 167
Registered: ‎11-12-2008
My Device: Not Specified

Re: How to get the current time in GMT?

[ Edited ]

I'm a big **bleep**...

 

 

 

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); //e.g. 2008-06-03T12:15:03Z
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
c.setTime(new Date(System.currentTimeMillis())); //now
String formattedDate = dateFormat.format(c, new StringBuffer(), null).toString(); //formatted in UTC/GMT time
System.out.println(formattedDate);

 

 

See this thread: http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=4714&query.id=610...

 

Message Edited by bo on 03-12-2009 11:06 AM
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to get the current time in GMT?

Thanks - new to me as well....
Contributor
Posts: 29
Registered: ‎04-01-2011
My Device: 9800
My Carrier: torch

Re: How to get the current time in GMT?

Hi

  can you please help me that how can I get the time in hh:mm style without dates and other strings..

 

Thanks in Advance

Thanks
With Best Regards
Ashutosh Srivatava
Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: How to get the current time in GMT?

Look at the SimpleDateFormat class and it shows the characters to use to format your string however you like.
Contributor
Posts: 10
Registered: ‎11-11-2011
My Device: NA
My Carrier: NA

Re: How to get the current time in GMT?

hi.. I need just the time HH:MMSmiley FrustratedS format.. how can i get it?

Highlighted
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to get the current time in GMT?

The solution was provided above.

 

Please refrain from posting in "solved" threads.