32

I'm trying to convert ZonedDateTime to milliseconds using below code.

LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zonedDateTime =ldt.atZone(ZoneId.of(""Asia/Kolkata""));
zonedDateTime.toInstant().toEpochMilli();

But this one returns milliseconds based on local time. And it's not considering ZoneId.

Let's say LocalDateTime("2019-04-10T05:30"). If I convert this to ZonedDateTime with Zone id ("Asia/Kolkata") then I'm getting ("2019-04-10T05:30+05:30[Asia/Kolkata]"). Then I convert to EpochMilli (1554854400000) = ("Wed Apr 10 2019 00:00:00") in UTC.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Arun
  • 510
  • 2
  • 5
  • 9
  • 2
    Could you clarify a bit what output you are getting and what you expect? It's a bit unclear what is wrong with the code. The way you're doing it should work. – TiiJ7 Apr 12 '19 at 06:33
  • Let's say LocalDateTime("2019-04-10T05:30") . If I convert this to ZonedDateTime with Zone id ("Asia/Kolkata") then I'm getting ("2019-04-10T05:30+05:30[Asia/Kolkata]"). Then I convert to EpochMilli (1554854400000) = ("Wed Apr 10 2019 00:00:00") in UTC. – Arun Apr 12 '19 at 06:39
  • 3
    Welcome to Stack Overflow. It's not clear to me what you're trying to achieve, or what you expect the results to be. If you're not in the Asia/Kolkata time zone, you're not finding the *current* epoch time, certainly. Please could you give more information about what you're trying to achieve, what values you're seeing and what you expect to see? – Jon Skeet Apr 12 '19 at 06:40
  • 4
    Well yes, 2019-04-10T05:30+05:30 and 2019-04-10T00:00:00Z represent the same instant in time. What result did you expect, and why? – Jon Skeet Apr 12 '19 at 06:41
  • Thanks for the supplementary information. It’s always welcome, and you should always add it in your question, not in a comment. Only this time I did it for you. – Ole V.V. Apr 12 '19 at 11:32
  • The conversion you describe is correct. Which result did you want to have instead? – Ole V.V. Apr 12 '19 at 11:34
  • I need "EpochMilli" for "Wed Apr 10 2019 05:30:00" in UTC. – Arun Apr 22 '19 at 06:42
  • Actually, I misunderstood from the beginning, I thought - 2019-04-10T05:30+05:30[Asia/Kolkata] this is equal to 2019-04-10T11:00 in [Asia/Kolkata]. Which is totally wrong. Now I'm cleared. Thank you, guys :) – Arun May 13 '19 at 06:35

2 Answers2

60

You are using an Instant to get that milliseconds representation. Instant are not zone based. Now, the epoch time is based on the "1970-01-01T00:00:00Z" so you should not have the zone in it.

If you want to create a ZoneDateTime from the epoch value, you can simply create an Instant at that epoch time and then create a ZonedDateTime with the zone you wish :

//Let's create our zone time (just to keep your logic
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zonedDateTime =ldt.atZone(ZoneId.of("Asia/Kolkata"));

//Then get the epoch on GMT
long e = zonedDateTime.toInstant().toEpochMilli();

Instant i = Instant.ofEpochMilli(e);
System.out.println(ZonedDateTime.ofInstant(i, ZoneId.systemDefault()));
System.out.println(ZonedDateTime.ofInstant(i, ZoneId.of("Asia/Kolkata")));

2019-04-12T05:10:31.016+02:00[Europe/Paris]
2019-04-12T08:40:31.016+05:30[Asia/Kolkata]

NOTE : The code above should not be used like this, it is not necessary to get a LocalDateTime then a ZonedDateTime to finally create an Instant. This is just to show that even with a zone, this will be "lost" at one point.
Simply use :

long e = Instant.now().toEpochMilli();
AxelH
  • 14,325
  • 2
  • 25
  • 55
  • The question is about converting ZonedDateTime to milliSec. Where is it converting to miliSec in specified timezone? – Ram Jul 03 '20 at 13:16
  • Checkout this one which actually serves the purpose. https://stackoverflow.com/questions/22990067/how-to-extract-epoch-from-localdate-and-localdatetime – Ram Jul 03 '20 at 14:38
  • @MAC, It is not fresh in my mind, but the point here was to demonstrate that the expected value was not "zone based". Note that the solution you shared do works (in second), but it becomes a "zoned epoch time" that could provided some issues to convert later. Simply because you need the numerical value AND the zone to be able to work with it. – AxelH Jul 08 '20 at 13:41
5
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zonedDateTime =ldt.atZone(ZoneId.of("UTC"));
zonedDateTime.toInstant().toEpochMilli();
BIG.Jung
  • 51
  • 1
  • 1
  • 2
    Welcome to SO! Could you explain how this solves the problem and why it's a good solution to help OP and future visitors? Thanks. – ggorlen Oct 16 '20 at 18:24