File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 237
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
It's not a secret anymore!