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 ?
TimestampextendsDate– 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.)