ThreeTen and parsing instant

I am using ThreeTen and trying to format Instant. It would be easier to just break it down, but I'm curious if this should work? From everything I've read, Instant should be syntactic and with all template components:

@Test
public void testInstants()  {
    Instant instant = Instant.now();
    String dbDatePattern = "YYYY-MM-dd HH:mm:ss.SSS";
    try {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dbDatePattern);
        String dbDate = formatter.format(instant);
    } catch (Exception ex) {
        int dosomething = 1;
    }
}

      

Error: org.threeten.bp.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfWeek

dd is the day of the month, not DayofWeek. A red herring is probably thrown, but it seems strange.

+3


source to share


2 answers


The letter "Y" stands for the weekly year in ThreeTen-Backport and JSR-310 (which meant that in the Yoda era, this is the time of year). To calculate a weekly year, a day of the week is needed, hence an error.



Note that Instant

you cannot provide fields for the formatting that you are trying to create. Only ZonedDateTime

, LocalDateTime

or OffsetDateTime

they can. Instant

is a special case that needs to be formatted with DateTimeFormatter.ISO_INSTANT

or similar.

+3


source


To make JodaStephen's explicit answer:

String dbDatePattern = "YYYY-MM-dd HH:mm:ss.SSS";

(uppercase YYYY)

it should be

String dbDatePattern = "YYYY-MM-dd HH:mm:ss.SSS";

(lowercase yyyy)

instead.

Also, instead of



Instant instant = Instant.now();

do

LocalDateTime localDateTime = LocalDateTime.now();

... and then pass that instead format()

.

Since Instant

u LocalDateTime

implements TemporalAccessor

what it means DateTimeFormatter.format()

, the rest of your code should work as is.

+1


source







All Articles