Created
April 11, 2018 09:13
-
-
Save diversit/0adb8cb9fadd7f8759587603d587213d to your computer and use it in GitHub Desktop.
Proper deserialisation of Java 8 Time
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
/** | |
* Java 8 does not full support ISO8601 date/time format. (https://en.m.wikipedia.org/wiki/ISO_8601) | |
* It does not support timezone '±HHMM' or '±HH'. | |
* This has been reported in issue https://bugs.openjdk.java.net/browse/JDK-8032051, | |
* but has only been fixed in Java 9, not in Java 8! | |
* | |
* This object provides an alternative [[Reads]] instance which fully supports ISO8601 formats, | |
* which should be used instead of Play Json's [[play.api.libs.json.EnvReads.DefaultInstantReads]]. | |
*/ | |
object ISO8601 { | |
import java.time.format.{ DateTimeFormatter, DateTimeFormatterBuilder } | |
// formatter for timezone ±[hh][mm] | |
private val ISO8601_ALT1 = new DateTimeFormatterBuilder() | |
.parseCaseInsensitive() | |
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) | |
.appendOffset("+HHMM", "Z") | |
.toFormatter() | |
// formatter for timezone ±[hh] | |
private val ISO8601_ALT2 = new DateTimeFormatterBuilder() | |
.parseCaseInsensitive() | |
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) | |
.appendOffset("+HH", "Z") | |
.toFormatter() | |
val InstantReads = | |
Reads.DefaultInstantReads orElse // default formatter only partially supports ISO8601. See https://bugs.openjdk.java.net/browse/JDK-8032051 | |
Reads.instantReads(ISO8601_ALT1) orElse | |
Reads.instantReads(ISO8601_ALT2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment