JSR-310 – parsing seconds fraction with variable length

This solves the problem: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern(“yyyy-MM-dd HH:mm:ss”) .appendFraction(ChronoField.MICRO_OF_SECOND, 0, 6, true) .toFormatter(); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22.276052”, formatter)); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22.276”, formatter)); System.out.println(LocalDateTime.parse(“2015-05-07 13:20:22”, formatter)); // output 2015-05-07T13:20:22.276052 2015-05-07T13:20:22.276 2015-05-07T13:20:22 The answer by JiriS is incorrect, as it uses appendValue whereas the correct way is to use DateTimeFormatterBuilder.appendFraction (which also handles the decimal point). The … Read more

DateTimeFormatter month pattern letter “L” fails

“stand-alone” month name I believe ‘L’ is meant for languages that use a different word for the month itself versus the way it is used in a date. For example: Locale russian = Locale.forLanguageTag(“ru”); asList(“MMMM”, “LLLL”).forEach(ptrn -> System.out.println(ptrn + “: ” + ofPattern(ptrn, russian).format(Month.MARCH)) ); Output: MMMM: марта LLLL: Март There shouldn’t be any reason … Read more

Is there a reason for java.time.ZoneId not including an Enum of ZoneIds?

I believe it’s because the list of all possible timezones names can change regardless of Java version. Timezone information comes with Java installation (usually in the folder <java-home>/lib/zi, or in jre/lib/tzdb.dat file in newer versions). But this information can be updated without changing the Java version (using the Timezone Updater Tool). If the timezone data … Read more

Deserialize millisecond timestamp to java.time.Instant

Solution was to add .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false) to the ObjectMapper. Complete ObjectMapper looks like: ObjectMapper om = new ObjectMapper() .registerModule(new JavaTimeModule()) .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false) .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false) .setSerializationInclusion(Include.NON_NULL);

Java SE 8 TemporalAccessor.from issues when used with a java.time.Instant object

Short answer: The JSR-310-designers don’t want people to do conversions between machine time and human time via static from()-methods in types like ZoneId, ZoneOffset, OffsetDateTime, ZonedDateTime etc. This is explicitly specified if you carefully study the javadoc. Instead use: OffsetDateTime#toInstant():Instant ZonedDateTime#toInstant():Instant Instant#atOffset(ZoneOffset):OffsetDateTime Instant#atZone(ZoneId):ZonedDateTime The problem with the static from()-methods is that otherwise people are able … Read more

Is java.time failing to parse fraction-of-second?

Bug – Fixed in Java 9 This issue was already reported in JDK-bug-log. Stephen Colebourne mentions as work-around following solution: DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendPattern(“yyyyMMddHHmmss”) .appendValue(ChronoField.MILLI_OF_SECOND, 3) .toFormatter(); Note: This workaround does not cover your use-case of only two pattern symbols SS. An adjustment might only be to use other fields like MICRO_OF_SECOND (6 … Read more