How to make date.getTime() returns UTC time?

date, java, utc

Solution

tl;dr

Instant.now()

…and…

Instant.ofEpochMilli( n ) 

…and…

instant.toEpochMilli()

`Date` is always in UTC

When I use the method getTime() to get the long value of this object, the value returned corresponds to our local time (central US).

No, the value returned from `Date::getTime()` always corresponds to UTC (virtually the same thing as GMT in this context). To quote the class doc:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

So your Question is nonsensical in that a `Date` is already in UTC. No need to “get the value back which corresponds to the original UTC time”,

You may be confusing the behavior of `getTime` with that of the `toString` method. The `toString` method annoyingly and confusingly applies the current default time zone in the process of generating the String. So the string output appears with a time zone while in fact there is no time zone to be set or gotten from the `Date` itself. (There actually is a zone deep within the `Date` but that is irrelevant to this discussion here. This class is a confusing mess!)

java.time

The modern way to do this is using java.time classes.

`Instant`

The `Instant` class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Get the current moment.

Instant instant = Instant.now();

You can convert a `Date` to its modern replacement by calling one of the new conversion methods added to the old date-time classes. Just call `toInstant`, quite easy.

Instant instant = myJavaUtilDate.toInstant();  

I do not recommend at all using a count-from-epoch number as a way of tracking time. Stick with the java.time objects instead. When outside Java, serialize to text use the ISO 8601 formats.

But if you must, you can extract a count of milliseconds since epoch of 1970-01-01T00:00:00Z. Note that this may involve data loss! An `Instant` has a finer resolution of nanoseconds. So going to milliseconds may lop off a fraction of the fraction of a second.

long millisecondsSinceEpoch = instant.toEpochMilli();  // Caution: Possible data-loss in going from nanoseconds to milliseconds.

Going the other direction, from a count to an `Instant`.

Instant instant = Instant.ofEpochMilli( millisecondsSinceEpoch ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as `java.util.Date`, `Calendar`, & `SimpleDateFormat`.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

- Java SE 8 and SE 9 and later

- Built-in.

- Part of the standard Java API with a bundled implementation.

- Java 9 adds some minor features and fixes.

- Java SE 6 and SE 7

- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.

- Android

- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.

- See How to use….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as `Interval`, `YearWeek`, `YearQuarter`, and more.

Problem

I have a Date object which represents a UTC time. When I use the method getTime() to get the long value of this object, the value returned corresponds to our local time (central US). What is the correct way to get the value back which corresponds to the original UTC time? Thanks

Original source

Related problems