27

I've been trying to convert a "DateTime" to milliseconds using the java.time package built into Java 8.

But I haven't been able to do it correctly. I am trying to convert "29/Jan/2015:18:00:00" to milliseconds. The following is something I tried

Instant instant = Instant.parse("2015-01-29T18:00:00.0z");
Long instantMilliSeconds = Long.parseLong(instant.getEpochSecond() + "" + instant.get(ChronoField.MILLI_OF_SECOND));
System.out.println(new Date(instantMilliSeconds)); // prints Sun Jun 14 05:06:00 PDT 1970

I tried using LocalDateTime, but couldn't find a way to effectively do the conversion to milliseconds. I am not saying this is the best way to do this, if you know something better, I would really appreciate some pointers.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Seagull
  • 2,219
  • 6
  • 25
  • 33
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – vanza Feb 12 '15 at 03:26
  • Possible duplicate of [How to get milliseconds from LocalDateTime in Java 8](https://stackoverflow.com/questions/23944370/how-to-get-milliseconds-from-localdatetime-in-java-8) – Vadzim Mar 03 '18 at 08:04

3 Answers3

48

You should use Instant::toEpochMilli.


System.out.println(instant.toEpochMilli());
System.out.println(instant.getEpochSecond());
System.out.println(instant.get(ChronoField.MILLI_OF_SECOND));

prints

1422554400000
1422554400
0

Your version did not work because you forgot to pad instant.get(ChronoField.MILLI_OF_SECOND) with extra zeros to fill it out to 3 places.

David Siro
  • 1,826
  • 14
  • 33
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • Thank you, the "toEpochMilli" solved this for me, however I would have to convert ""29/Jan/2015:18:00:00"" to the ""2015-01-29T18:00:00.0z" format for this to work with the Instant class. I am curious to know what would be the best way to convert "29/Jan/2015:18:00:00" to milliseconds – Seagull Feb 12 '15 at 00:02
  • 4
    [Ask another question](https://meta.stackexchange.com/questions/39223/one-post-with-multiple-questions-or-multiple-posts) and I'm sure someone will be able to help you. – Jeffrey Feb 12 '15 at 00:06
3

From Date and Time Classes the tutorials...

DateTimeFormatter formatter
                    = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss");
LocalDateTime date = LocalDateTime.parse("29/Jan/2015:18:00:00", formatter);
System.out.printf("%s%n", date);

Prints 2015-01-29T18:00

ZoneId id = ZoneId.systemDefault();
ZonedDateTime zdt = ZonedDateTime.of(date, id);
System.out.println(zdt.toInstant().toEpochMilli());

Prints 1422514800000

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks for your time, I could've certainly done this, but I was looking for something more concise.The answer I posted is more or less what I was looking for – Seagull Feb 12 '15 at 03:38
  • @Seagull So, apart from chaining the object creation through the methods, it's the same thing...except, you could reduce it to a single line of code, but you lose readability...You also lose re-usability, but that's just me... – MadProgrammer Feb 12 '15 at 03:40
  • You're right about loosing readability through chaining of methods, however, the bottom line is that I was looking for a way to do more with less. I feel I acheived that with my solution, since it is coupled to lesser number of classes – Seagull Feb 12 '15 at 09:02
  • Arguably you've still used the same number of classes, you've just wrapped them in the method calls. And we could spend the rest of our lives auguring if it's less or more efficient, but it gave me chance to explore the api a little more :P – MadProgrammer Feb 12 '15 at 09:48
  • Unrelated to the topic. I liked MadProgrammer's comment about losing readibility. very true, I wonder what'd you say about Java stream() API, chaining those methods to high heaven. – Vortex Nov 17 '16 at 20:30
1

Okay, I think I finally found an easy way to do what I am trying to do

LocalDateTime localDateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("dd/MMM/uuuu:H:m:s"));
System.out.println(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli());

Prints 1390903200000

Seagull
  • 2,219
  • 6
  • 25
  • 33
  • There is another [link](http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-date) which does a good job of explaining what needs to be done here – Seagull Feb 12 '15 at 18:27