Last active
August 17, 2016 21:02
-
-
Save bastman/02da5665c0228a6f6dbc5b1662aa0ca7 to your computer and use it in GitHub Desktop.
Some example shortcuts for working with Java8 DateTime Api (java.time): e.g. Instant, LocalDateTime, ZonedDateTime, OffsetDateTime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.util | |
import groovy.transform.CompileStatic | |
import groovy.transform.TypeChecked | |
import java.time.* | |
import java.time.format.DateTimeFormatter | |
@CompileStatic | |
@TypeChecked | |
class InstantUtcUtil { | |
public static final List<Closure<Instant>> PARSERS_DEFAULT = [ | |
{ | |
String text -> return Instant.parse(text) | |
}, | |
{ | |
String text -> | |
return ZonedDateTime.parse(text) | |
.toInstant() | |
}, | |
{ | |
String text -> | |
return OffsetDateTime.parse(text) | |
.toInstant() | |
}, | |
{ | |
String text -> | |
return LocalDateTime.parse(text) | |
.toInstant(ZoneOffset.UTC) | |
}, | |
{ | |
String text -> | |
return LocalDateTime.parse(text, DateTimeFormatter.ISO_ZONED_DATE_TIME) | |
.toInstant(ZoneOffset.UTC) | |
}, | |
{ | |
String text -> | |
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmX")) | |
.toInstant(ZoneOffset.UTC) | |
} | |
]; | |
public static Instant parseOrElseGet(String value, List<Closure<Instant>> parserList, Closure<Instant> orElse) { | |
Objects.requireNonNull(value, "value") | |
Objects.requireNonNull(parserList, "matchers") | |
Objects.requireNonNull(orElse, "orElse") | |
for (parser in parserList) { | |
try { | |
Instant instant = (Instant) parser(value) | |
if (instant != null) { | |
return instant | |
} | |
} catch (all) { | |
// ignore, handled graceful | |
} | |
} | |
return orElse() | |
} | |
public static Instant toInstant(LocalDateTime localDateTime) { | |
Objects.requireNonNull("localDateTime") | |
return localDateTime.toInstant(ZoneOffset.UTC) | |
} | |
public static Instant toInstant(ZonedDateTime zonedDateTime) { | |
Objects.requireNonNull("zonedDateTime") | |
return zonedDateTime.toInstant() | |
} | |
public static Instant toInstant(OffsetDateTime offsetDateTime) { | |
Objects.requireNonNull("offsetDateTime") | |
return offsetDateTime.toInstant() | |
} | |
public static LocalDateTime toLocalDateTime(Instant instant) { | |
Objects.requireNonNull("instant") | |
return LocalDateTime.ofInstant(instant, ZoneOffset.UTC) | |
} | |
public static ZonedDateTime toZonedDateTime(Instant instant) { | |
Objects.requireNonNull("instant") | |
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC) | |
} | |
public static OffsetDateTime toOffsetDateTime(Instant instant) { | |
Objects.requireNonNull("instant") | |
return OffsetDateTime.ofInstant(instant, ZoneOffset.UTC) | |
} | |
public static Instant withEpochSecondPrecision(Instant source) { | |
Objects.requireNonNull(source, "source") | |
return Instant.ofEpochSecond(source.getEpochSecond(), 0) | |
} | |
public static Instant requireNonEmpty(Object source, String message) { | |
boolean isValid = source instanceof Instant && source.getEpochSecond() > 0; | |
boolean hasMessage = message instanceof String && !(message.trim().isEmpty()); | |
String finalMessage = "Parameter must be Instant, not empty!"; | |
if (hasMessage) { | |
finalMessage += " " + message | |
} | |
if (!isValid) { | |
throw new RuntimeException(finalMessage); | |
} | |
return (Instant) source; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
System.out.println(ZonedDateTime.parse("2016-08-17T22:00:00Z").toString()); // 2016-08-17T22:00Z
System.out.println(ZonedDateTime.parse("2016-08-17T22:00:00.123Z").toString()); // 2016-08-17T22:00:00.123Z
System.out.println(ZonedDateTime.parse("2016-08-17T22:00:00+02:00").toString()); // 2016-08-17T22:00+02:00
System.out.println(ZonedDateTime.parse("2016-08-17T22:00:00+02:00").withZoneSameInstant(ZoneId.of("UTC")).toString()); // 2016-08-17T20:00Z[UTC]
System.out.println(ZonedDateTime.parse("2016-08-17T22:00:00").toString()); // exception, weil ZeitZoneInfo fehlt.