aspose file tools*
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 JavaScript Promises Essentials this week in the JavaScript 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!
 
Consider Paul's rocket mass heater.
 
subject: GMT