javaandroiddatetimeiso8601threetenbp

How can I change java.util.Date to ISO String using ThreeTenABP


I'm using ThreeTenABP for converting date time for Android. My question is how can I change a java.util.Date to an ISO String (format is 2018-05-24T02:33:10.062Z) by ThreeTenABP?


Solution

  • A ThreetenABP-solution can look like this:

    java.util.Date d = ...;
    org.threeten.bp.Instant instant = org.threeten.bp.DateTimeUtils.toInstant(d);
    String iso = instant.toString();
    

    If you wish more control over formatting then you can convert the instant to a ZonedDateTime (or better to an OffsetDateTime) and use a dedicated DateTimeFormatter.