0

I am using the new Java LocalDateTime class as well as the Instant class. I have a database where the timestamp is stored as a long.

The question is why I have to use a ZoneDateTime When I can specifiy the ZoneID via a ofInstant from LocalDatetime

LocalDateTime.ofInstant(Instant.ofEpochMilli(1563387286000l),ZoneId.of("America/Panama"));

I am using this formatter

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV O");

This line produces throws an exception (Unable to extract value: class java.time.LocalDateTime)

localDateTime.format(dateTimeFormatter);

To fix the issue I had to use this code. It works fine.

LocalDateTime localDateTime =LocalDateTime.ofInstant(Instant.ofEpochMilli(1563387286000l),ZoneId.of("America/Panama"));
ZonedDateTime zone = ZonedDateTime.of(localDateTime,ZoneId.of("America/Panama"));
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV O");
localDateTime.format(dateTimeFormatter);

Based On what I have learned from Java DOC (Oracle)

LocalDateTime class represents a value without the ZoneID.

ofInstant method allows to obtain a LocalDateTime with the ZoneID(Offset) comprised.

My question: why can't we use LocalDatetime with the follwing formatter : DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss VV O");

This is fishy, maybe they have updated in the recent JDKs

Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
  • 1
    _LocalDateTime class represents a value without the ZoneID_ What does `VV` in your pattern represent? – Sotirios Delimanolis Nov 20 '19 at 22:20
  • @SotiriosDelimanolis VV represents the time-zone ID . You have a full list of all possible value here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html – Nesan Mano Nov 20 '19 at 22:23
  • 2
    So, how could you transform a LocalDateTime, which doesn't have any ZoneId, into a string containing a ZoneId? Where would the ZoneId come from? – JB Nizet Nov 20 '19 at 22:24
  • @JBNizet Your sample context would definitely require a ZoneDateTime where you specify the ZoneID. – Nesan Mano Nov 20 '19 at 23:36

0 Answers0