Get Gmt Time in Java

How can I get the current date and time in UTC or GMT in Java?

java.util.Date has no specific time zone, although its value is most commonly thought of in relation to UTC. What makes you think it's in local time?

To be precise: the value within a java.util.Date is the number of milliseconds since the Unix epoch, which occurred at midnight January 1st 1970, UTC. The same epoch could also be described in other time zones, but the traditional description is in terms of UTC. As it's a number of milliseconds since a fixed epoch, the value within java.util.Date is the same around the world at any particular instant, regardless of local time zone.

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, or a SimpleDateFormat instance, which, by default, also uses 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.

How to find epoch format current time of GMT using java

Why not use Calendar class?

public long getEpochTime(){
return Calendar.getInstance(TimeZone.getTimeZone("GMT-7")).getTime().getTime()/1000; //( milliseconds to seconds)
}

It'll return the current Date's Epoch/Unix Timestamp.

Based on Harald's Comment:

 public static long getEpochTime(){
return Clock.system(TimeZone.getTimeZone("GMT-7").toZoneId() ).millis()/1000;
}

Java time in GMT

This accentuates why Java sucks at time. The previous posts are all close, but we need to be very careful about getting the current time in GMT, and getting the current time in CDT and calling it GMT.

TimeZone reference = TimeZone.getTimeZone("GMT");
Calendar myCal = Calendar.getInstance(reference);

This is the current time in GMT, with a timezone context of GMT.

To convert to a Date object which keeps the zone intact you'll need to call:

TimeZone.setDefault(reference);

This setting will last as long as your current JVM. Now calling get Time should produce the desired result.

myCal.getTime();

Getting GMT time with Android

As far as I read the calendar.getTimeInMillis(); returns the UTC time in millis. I used the following code and compared it to the Epoch in this site http://www.xav.com/time.cgi.

public int GetUnixTime()
{
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
int utc = (int)(now / 1000);
return (utc);

}

Giora

Get GMT Time in Milliseconds using JAVA

Use Calendar#getTimeInMillis:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long time = cal.getTimeInMillis();

How to Get GMT by using Google Time Zone API

rawOffset is the offset from UTC (in seconds) for the given location.

As UTC and GMT has no time difference between them, dividing rawOffset by 3600 you can get the GMT time of your requested time zone.

And obviously, when divided by 3600, the answer is in fractions of hour not in the hour:min pair. So, if you get the output, say, 4.50, that is actually 4.5 hours which is the same as 4:30 hours (4 hours 30 minutes).



Related Topics



Leave a reply



Submit