This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes GMT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GMT" Watch "GMT" New topic
Author

GMT

Kjartan Reynisson
Greenhorn

Joined: Mar 31, 2006
Posts: 4
I need to create GregorianCalendar GMT time, but the below code retunrs my timezone CEST.


Can anyone show me how to get GTM?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

Here's the scoop: Calendar's getTime method returns a Date object:

Date getTime() //not the best method name, eh?

... and Date's state don't include a time zone. Its toString method implicitly uses your JVM's default time zone! WTF?!

To get around this, use a DateFormat, but don't forget to set it's TimeZone property first!

You can also avoid Calendar and go directly with Date if you just want the current date:

DateFormat df = DateFormat.getDateTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date();
System.out.println("now = " + date);
System.out.println("now in GMT = " + df.format(date));
[ April 03, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GMT
 
Similar Threads
settimezone on gregorian calendar doesn't work !!
Daylight saving problem
Problem on great datetime in GMT
calendar timezone
Question about using a specific TimeZone to set a GregorianCalendar...