I am using TrueTime library which returns Date
at the system time zone. I am having problems converting this Date
to UTC Date when converted to milliseconds.
Here is what I did:
// getting true time in GMT [ex : 2020-07-13T18:00:57.192+03:00 [Europe/Istanbul]]
Date now = getTrueNowTimeInGMT();
// I like to use `LocalDateTime`, so I am converting `Date` to `LocalDateTime`:
LocalDateTime ldtOfSystem = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
// Converting system default zone to UTC
ZonedDateTime zdtOfSystem = ldtOfSystem.atZone(ZoneId.systemDefault());
ZonedDateTime zdtOfSystemInUTC = zdtOfSystem.withZoneSameInstant(ZoneId.of("UTC"));
// Making sure that it is in the UTC. It is returning correct UTC time [ex: 2020-07-13T15:00:57.192 [UTC]]
System.out.println(zdtOfSystemInUTC);
// converting zdtOfSystemInUTC to milliseconds - problem is occurring here!
zdtOfSystemInUTC.toInstant().toEpochMilli();
I am losing zone data and it is returning milliseconds based on the local zone (system zone). I am converting these milliseconds to Date again and the result is in GMT+3 [2020-07-13T18:00:57.192+03:00]
Am I missing something? I read in one post that toInstant()
method doesn't care about the timezone. How can I get the milliseconds from the specific time zone I pointed out in ZonedDateTime
?
EDIT I changed variable names (hopefully, into better ones) thanks to @MenuHochSchild for pointing that out.
Clarification. Why do I have the need for the UTC time?
I cannot control the user's time. He/she can change date and time of his/her device easily and that is creating problems for us. So in order to tackle the problem, we found a library called TrueTime which gives true-time Date
object like this:
Tue Jul 14 00:32:46 GMT+03:00 2020
In order to sync with our server and do some time-related operations, I need to convert this Date
which is GMT to UTC and convert it to milliseconds.
You seem to have misunderstood a thing or two.
A
Date
hasn’t got any time zone or offset from UTC or GMT. So it cannot be in GMT. ADate
is a point in time, nothing more, nothing less. As an aside, I would normally recommend you don’t use that class at all since it is poorly designed and long outdated; but if TrueTime cannot yet give you anInstant
or other modern class, we’ll have to go withDate
for now.A count of milliseconds since the epoch is independent of time zone. The epoch is usually defined in UTC, but it is one point in time. Therefore the milliseconds are never based on any time zone (or you may say that they are always based on UTC, depending on how you prefer to use the words, it means the same).
I didn’t understand this part. You are quoting the same date, time and UTC offset as the one you got from TrueTime, so how could it be wrong? Java regards UTC and GMT as synonymous (in the real world there’s always less than a second between UTC and GMT, so this is probably OK).
Still assuming that you cannot get but on old-fashioned
Date
from your date and time library, it seems to me that the easy way to get the current number of milliseconds since the epoch is:Because of what I said above this will be reliable no matter the time zone setting of the device.