Getting today's date in GMT time zone

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!

[792 byte] By [GSPa] at [2007-11-27 6:18:01]
# 1

A Date models a point in time which is the same across the globe. Therefore the Date class does not have the concept of a time zone. Time zone is applied only when you format a Date object for presentation.

If you want to access the different parts of a time such as the date and the hour of day in UTC time zone (also known as GMT in some parts of the world) you can use the Calendar class.

jsalonena at 2007-7-12 17:31:36 > top of Java-index,Java Essentials,Java Programming...
# 2
OK. Thanks! I'll try using the Calendar class.
GSPa at 2007-7-12 17:31:36 > top of Java-index,Java Essentials,Java Programming...
# 3

I don't think Calendar is useful here.

"Current date as GMT" is meaningless. Current date (and time) is just "now." Timezone is not part of the picture. If you want to present the current date/time in GMT, then....

Date date = new Date();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm z");

System.out.println(df.format(date));

df.setTimeZone(TimeZone.getTimeZone("GMT"));

System.out.println(df.format(date));

2007-06-04 02:29 PDT

2007-06-04 09:29 GMT

jverda at 2007-7-12 17:31:36 > top of Java-index,Java Essentials,Java Programming...