This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GMT

 
Kjartan Reynisson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic