• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting current GMT date as Date object

 
G Priya
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 240
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic