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
Could Not Find Method Compile() for Arguments Gradle
How to Make Line Animation Smoother
Difference Between Oracle Jdbc Driver Classes
Openjdk Availability for Windows Os
What Hashing Function Does Java Use to Implement Hashtable Class
What Does "Atomic" Mean in Programming
How to Remove a Key from Hashmap While Iterating Over It
Difference Between List and Array
Java.Lang.Noclassdeffounderror: Could Not Initialize Class Xxx
When to Catch the Exception VS When to Throw the Exceptions
Java.Nio.File.Path for a Classpath Resource
Inter Thread Communication in Java
Grab a Segment of an Array in Java Without Creating a New Array on Heap
Why Can This Generic Method with a Bound Return Any Type
Java Int to String - Integer.Tostring(I) VS New Integer(I).Tostring()