Jackson Serialize Instant to Nanosecond Issue

When we are working with Java 8 Time package and Jackson good idea is to use jackson-modules-java8 project which serves many serialisers and deserialisers ready to use. To enable it we need to register JavaTimeModule module. To serialise Instant InstantSerializer is used. When we check how it is implemented we will find out that behind … Read more

Why can’t OffsetDateTime parse ‘2016-08-24T18:38:05.507+0000’ in Java 8

You are calling the following method. public static OffsetDateTime parse(CharSequence text) { return parse(text, DateTimeFormatter.ISO_OFFSET_DATE_TIME); } It uses uses DateTimeFormatter.ISO_OFFSET_DATE_TIME as DateTimeFormatter which, as stated in the javadoc, does the following: The ISO date-time formatter that formats or parses a date-time with an offset, such as ‘2011-12-03T10:15:30+01:00’. If you want to parse a date with … Read more

How to convert LocalDate to SQL Date Java?

The answer is really simple; import java.sql.Date; … LocalDate locald = LocalDate.of(1967, 06, 22); Date date = Date.valueOf(locald); // Magic happens here! r.setDateOfBirth(date); If you would want to convert it the other way around, you do it like this: Date date = r.getDate(); LocalDate localD = date.toLocalDate(); r is the record you’re using in JOOQ … Read more

Formatting a Duration in Java 8 / jsr310

Java 9 and later: Duration::to…Part methods In Java 9 the Duration class gained new to…Part methods for returning the various parts of days, hours, minutes, seconds, milliseconds/nanoseconds. See this pre-release OpenJDK source code. Given a duration of 49H30M20.123S… toNanosPart() = 123000000 toMillisPart() = 123 toSecondsPart() = 20 toMinutesPart() = 30 toHoursPart() = 1 toDaysPart() = … Read more

Parsing string to local date doesn’t use desired century

Since this question is really about new java.time-package and NOT SimpleDateFormat I will cite following relevant section: Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. … Read more

How can I parse/format dates with LocalDateTime? (Java 8)

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern. String str = “1986-04-08 12:30”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”); LocalDateTime dateTime = LocalDateTime.parse(str, formatter); Formatting date and … 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