2

My need is that I have a value in Long, which represent the milliseconds value since epoch. I wish to find out the difference in the number of days between that day and the current day. I am using Java8's DAYS.between(inputDate, currentDate)

For the currentDate I have used LocalDateTime currentDate = LocalDateTime.now(); But the issue I am facing is when I am converting the long value into java8 LocalDateTime. When I use

LocalDate date = Instant.ofEpochMilli(1490372528)
  .atZone(ZoneId.systemDefault())
  .toLocalDate();

The result is 1970-01-18 while when I enter the same value in https://www.epochconverter.com/ it gives Fri, 24 Mar 2017 16:22:08 GMT

Why is this discrepancy there? How to effectively get a java8 date from a long value to use in DAYS.between()?

assylias
  • 321,522
  • 82
  • 660
  • 783
aayush_v20
  • 191
  • 1
  • 2
  • 8
  • 3
    From the page you linked to: *The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of **seconds** that have elapsed since January 1, 1970)*. ofEpochMilli, as its name indicates, expects **milliseconds**. Use https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#ofEpochSecond-long- – JB Nizet May 04 '17 at 17:27

1 Answers1

7

You have to decide. Either, your number is “milliseconds value since epoch”, as you stated in your question, or it is “number of seconds that have elapsed since January 1, 1970”, as stated (and used) on the linked web site.

If it is truly “milliseconds since epoch”, you can use

System.out.println(Instant.ofEpochMilli(1490372528).until(Instant.now(), ChronoUnit.DAYS));

to print the number of days, which is, by the way, not simpler than the pre-Java 8 code

System.out.println(TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis()-1490372528));

If your number actually is “seconds since epoch”, you have little to change in your code

System.out.println(Instant.ofEpochSecond(1490372528).until(Instant.now(),ChronoUnit.DAYS));

which is equivalent to

System.out.println(ChronoUnit.DAYS.between(Instant.ofEpochSecond(1490372528),Instant.now()));

as you mentioned DAYS.between explicitly. It will just delegate to the until method used above. The equivalent pre-Java 8 code would be slightly more complicated

System.out.println(TimeUnit.MILLISECONDS.toDays(
                       System.currentTimeMillis()-TimeUnit.SECONDS.toMillis(1490372528)));
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Great answer. Also, what do you think about this answer? https://stackoverflow.com/a/20811441/4358787 – Gaurav Jul 02 '18 at 10:59
  • @gaurav well, it does the job, but I would try to avoid such manual calculation, as there are classes like `TimeUnit` or `Duration` which already do the job, i.e. for two `Date` instances, you could just use `System.out.println(Duration.between(d2.toInstant(), d1.toInstant()));`. – Holger Jul 02 '18 at 11:22
  • Makes sense to avoid such calculations. As, I am using Java 1.7, I will go with TimeUnit class's methods. – Gaurav Jul 02 '18 at 12:16