This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GMT" Watch "GMT" New topic


Kjartan Reynisson

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();
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!
I agree. Here's the link:
subject: GMT
It's not a secret anymore!