views:

129

answers:

4

greetings all i am using the following method to get the current time in GMT timezone

public static Timestamp getCurrentTimeGMT() {

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        long time = c.getTimeInMillis();
        long offset = TimeZone.getDefault().getOffset(time);
        return new Timestamp(time - offset);

    }

but when i try to use the same method with minor changes to get the current time in GMT+3 it gives me the same result of GMT ? i don't know why:

public static Timestamp getCurrentTimeGMT3() {

    Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+3"));
    long time = c.getTimeInMillis();
    long offset = TimeZone.getDefault().getOffset(time);
    return new Timestamp(time - offset);

}

any ideas why the above code doesn't work properly, and how to do such a method ?

+3  A: 

Timestamp extends Date - it doesn't have a time zone, conceptually. It just represents an instant in time.

If you want to display it in a particular calendar with a particular time zone, that's a formatting issue. Create the appropriate calendar with the relevant time zone, and set the timestamp within it accordingly.

(As per normal, I'd like to recommend that you use Joda Time instead of the built-in API where possible. It's much cleaner.)

Jon Skeet
...and has the concept of a LocalDateTime.
Michael Borgwardt
can you tell me how to do such a behaviour using joda time ?
sword101
@sword101: What behaviour *exactly*? If you just want an instant representing the current time (with no associated time zone) use `new Instant()`. If you want the current time and an associate with UTC, use `new DateTime(DateTimeZone.Utc)`.
Jon Skeet
i just want to get current time in the timezone (GMT+3)
sword101
+2  A: 

Why do you subtract the offset from the time in the last line? That is basically resetting your time back to GMT. You retrieve GMT+3 then you subtract 3 hours from that.

BruteForce
He is subtracting the offset of the default time zone, not the time zone he used to create the Calendar with.
jarnbjo
+2  A: 

What Jon said. A Timestamp does not have a time zone, it's always UTC, and really shouldn't be abused for representing local time. If you really need objects to represent local time, Joda Time has a class for that.

And you should be aware that "GMT+3" is not a real valid time zone. A time zone has not just a base offset, but also a daylight savings time offset, which can be different for time zones with the same base offset, and can even change for the same time zone due to legislation. A real time zone ID is "Europe/Berlin" or "Australia/Darwin".

Michael Borgwardt
A: 

c.getTimeInMillis() returns the number of milliseconds since January 1st, 1970 0:00 UTC and will return the same value, no matter which time zone is used in the Calendar instance.

If you create a Calendar with a time zone and need access to the time fields, you should access these directly:

int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
jarnbjo