tags:

views:

10534

answers:

7

When I create a new Date object, it is initialized to current time but in the local timezone. How can I get a simply the cuurent date and time in the GMT timezone?

+9  A: 

java.util.Date is always in UTC. What makes you think it's in local time? I suspect the problem is that you're displaying it via an instance of Calendar which uses the local timezone, or possibly using Date.toString() which also uses the local timezone.

If this isn't the problem, please post some sample code.

I would, however, recommend that you use Joda Time anyway, which offers a much clearer API.

Jon Skeet
I used a new Date() object and inserted into a DB table. DB is MySQL and when I use UTC_TIMESTAMP() function in MySQL, it works correctly, but when I insert the value of a new Date(), it is inserted in my local timezone.
Behrang
Then that's probably a driver issue. You may need to set your connection to UTC, or something like that. I've seen problems like this before, but the problem is not in java.util.Date.
Jon Skeet
Thanks. You are right. I looked at connection properties of mysql but I couldn't find the correct configuration. I asked another question: How to store a java.util.Date into a MySQL timestamp field in the UTC/GMT timezone? If you can, please help me there.
Behrang
Unfortunately I don't know - it's ages since I've done *any* JDBC, and none on MySQL, ever...
Jon Skeet
@Downvoter: Care to comment? What exactly is incorrect in my answer?
Jon Skeet
+1  A: 

With:

Calendar cal = Calendar.getInstance();

Then cal have the current date and time.
You also could get the current Date and Time for timezone with:

Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("GMT-2"));

You could ask cal.get(Calendar.DATE); or other Calendar constant about others details.
Date and Timestamp are deprecated in Java. Calendar class it isn't.

damian
Certain methods and constructors of Date and Timestamp are deprecated, but the classes themselves are not.
R. Bemrose
A: 

You can use:

Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

Then all operations performed using the aGMTCalendar object will be done with the GMT time zone and will not have the daylight savings time or fixed offsets applied. I think the previous poster is correct that the Date() object always returns a GMT it's not until you go to do something with the date object that it gets converted to the local time zone.

mjh2007
A: 
    Calendar c = Calendar.getInstance();
    System.out.println("current: "+c.getTime());

    TimeZone z = c.getTimeZone();
    int offset = z.getRawOffset();
    int offsetHrs = offset / 1000 / 60 / 60;
    int offsetMins = offset / 1000 / 60 % 60;

    System.out.println("offset: " + offsetHrs);
    System.out.println("offset: " + offsetMins);

    c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
    c.add(Calendar.MINUTE, (-offsetMins));

    System.out.println("GMT Time: "+c.getTime());
Ahmad Nadeem
+2  A: 
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));

//Local time zone   
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

//Time in GMT
return dateFormatLocal.parse( dateFormatGmt.format(new Date()) );
Dan
+1  A: 

Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); Then all operations performed using the aGMTCalendar object will be done with the GMT time zone and will not have the daylight savings time or fixed offsets applied

Wrong!

Calendar aGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); aGMTCalendar.getTime(); //or getTimeInMillis()

and Calendar aNotGMTCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-2"));aNotGMTCalendar.getTime();

will return the same time. Idem for new Date(); //it's not GMT.

simpatico
A: 

Isn't the internal time value (ms since .....) always in UTC, and only "displayable" fields converted to the selected time zone ?

lec