`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?

Within the scope of java.time-package, we can say: It is safer to use “u” instead of “y” because DateTimeFormatter will otherwise insist on having an era in combination with “y” (= year-of-era). So using “u” would avoid some possible unexpected exceptions in strict formatting/parsing. See also this SO-post. Another minor thing which is improved by … Read more

How to round time to the nearest quarter hour in java?

Rounding You will need to use modulo to truncate the quarter hour: Date whateverDateYouWant = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(whateverDateYouWant); int unroundedMinutes = calendar.get(Calendar.MINUTE); int mod = unroundedMinutes % 15; calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod)); As pointed out by EJP, this is also OK (replacement for the last line, only … Read more

How to set format of string for java.time.Instant using objectMapper?

One solution is to use jackson-modules-java8. Then you can add a JavaTimeModule to your object mapper: ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule module = new JavaTimeModule(); objectMapper.registerModule(module); By default the Instant is serialized as the epoch value (seconds and nanoseconds in a single number): {“createdDate”:1502713067.720000000} You can change that by setting in the object mapper: … Read more

DateTimeFormatter Support for Single Digit Day of Month and Month of Year

From the documentation: Number: If the count of letters is one, then the value is output using the minimum number of digits and without padding. So the format specifier you want is M/d/yyyy, using single letter forms. Of course, it will still parse date Strings like “12/30/1969” correctly as for these day/month values, two digits … Read more

Is there a class in java.time comparable to the Joda-Time Interval?

Sorry for you, there is no equivalent in JSR-310 to JodaTime-Interval-class. I have doubts if this will ever come, but project lead Stephen Colebourne considers at least to support it in the scope of his external library Threeten-Extra, see this issue. If you are happy with JodaTime you should keep it. Not everything in JodaTime … Read more

Why does the new Java 8 Date Time API not have nanosecond precision? [duplicate]

The java.time API in general does have nanosecond precision. For example: DateTimeFormatter formatter = DateTimeFormatter .ofPattern(“yyyy-MM-dd’T’HH:mm:ss,nnnnnnnnnZ”); OffsetDateTime odt = OffsetDateTime.of(2015, 11, 2, 12, 38, 0, 123456789, ZoneOffset.UTC); System.out.println(odt.format(formatter)); Output: 2015-11-02T12:38:00,123456789+0000 However, it’s the clock value returned by OffsetDateTime.now() which is returning a value which only has milliseconds. From Clock implementation in Java 8: The clock … Read more

Java 8 Instant class not have plusHours method despite shown in Oracle Tutorial example code

The Oracle Tutorial is incorrect Unfortunately, the Oracle Tutorial is incorrect on this matter. That line of example code is simply wrong. Good catch on your part. This error is quite unfortunate as the Tutorial is otherwise a fine resource for learning and studying Java. Instant::plus The Instant class has no such method as plusHours … Read more

Is there a jackson datatype module for JDK8 java.time?

As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time. Since Jackson 2.6.0 the “old” JSR310Module is deprecated. It is replaced by JavaTimeModule. Maven dependency is the same (you can find the current version in Maven Central): <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.6.0</version> </dependency> You have to register the module like this: ObjectMapper mapper = new ObjectMapper(); … Read more