This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting current GMT date as Date object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting current GMT date as Date object" Watch "Getting current GMT date as Date object" New topic

Getting current GMT date as Date object

G Priya

Joined: Apr 30, 2007
Posts: 11

I have a simple question, but am confused how to do it:
I need the current GMT date (as a Date object) do I do it?

Date date = new Date();
This returns the current date but according to the timezone of the system time & not GMT.

Using SimpleDateFormat comes close but it returns as a String object:
SimpleDateFormat dateFormat = new SimpleDateFormat("some format");
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
Date date = new Date();
String dateAsString = dateFormat.format(date);

This gets the current GMT date but as a String object, but I want it as a Date object.

Can you please throw some light on this? I guess there should be simple way to do it or am overlooking something.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Have a look at the GregorianCalendar class. It has constructors that let you specify the Locale and TimeZone.
[ June 04, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 234
Hi Priya,
I think it's better to use Calendar class instead of DateFormat(which must be used for formatting purposes)

This is a sample code that would return a Date object with GMT time zone

Please go through the Calendar API Calendar API
I agree. Here's the link:
subject: Getting current GMT date as Date object
jQuery in Action, 3rd edition