What’s the difference between ZonedDateTime and OffsetDateTime?

Q: What’s the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: “OffsetDateTime, ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds … 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

IST mapped to wrong ZoneId in java.time library

First, if there’s any way you can avoid it, don’t rely on three letter time zone abbreviations. They are ambiguous, probably more often than not. When you say that IST should be mapped to Asia, it still leaves the choice between Asia/Tel_Aviv and Asia/Kolkata open (plus the aliases for those two, Asia/Jerusalem and Asia/Calcutta). In … Read more

Create Java DateTime Instant from microseconds

long timeMicros = 1_565_245_051_795_306L; Instant i = Instant.EPOCH.plus(timeMicros, ChronoUnit.MICROS); System.out.println(i); Output is: 2019-08-08T06:17:31.795306Z Edit: Rather than dividing and multiplying to convert microseconds to milliseconds and/or seconds I preferred to use the built-in support for microseconds. Also when explicitly adding them to the epoch feels a little hand-held. You already know how to convert Instant to … Read more

Force 4-digit-year in localized strings generated from `DateTimeFormatter.ofLocalized…` in java.time

There is no built-in method for what you want. However, you could apply following workaround: Locale locale = Locale.ENGLISH; String shortPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern( FormatStyle.SHORT, null, IsoChronology.INSTANCE, locale ); System.out.println(shortPattern); // M/d/yy if (shortPattern.contains(“yy”) && !shortPattern.contains(“yyy”)) { shortPattern = shortPattern.replace(“yy”, “yyyy”); } System.out.println(shortPattern); // M/d/yyyy DateTimeFormatter shortStyleFormatter = DateTimeFormatter.ofPattern(shortPattern, locale); LocalDate today = LocalDate.now(ZoneId.of(“America/Chicago”)); String output … Read more

Android N Java8 java.time

java.time package was added only in API 26 (Android O): https://developer.android.com/reference/java/time/package-summary.html UPDATE Starting with version 4.0 of the Android Gradle Plugin, you can use a subset of java.time APIs (along with a number of other Java 8 language APIs) without requiring a minimum API level for your app: https://developer.android.com/studio/write/java8-support#library-desugaring The following set of APIs are … Read more