aspose file tools*
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 Spring in Action this week in the Spring 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
Author

Getting current GMT date as Date object

G Priya
Greenhorn

Joined: Apr 30, 2007
Posts: 11
Hi,

I have a simple question, but am confused how to do it:
I need the current GMT date (as a Date object) ..how 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.

Thanks!
Paul Sturrock
Bartender

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: 185
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting current GMT date as Date object