Skip to content

Instantly share code, notes, and snippets.

@hoangtranwork
Last active October 12, 2018 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoangtranwork/ae4e7b264dbc3db6608ecb81c2f37642 to your computer and use it in GitHub Desktop.
Save hoangtranwork/ae4e7b264dbc3db6608ecb81c2f37642 to your computer and use it in GitHub Desktop.
Java/Kotlin DateTimeFormatter to parse Apple version of RFC3339 date returned by Apple API (Itune receipt)
val appleRfc3339DateFormatter: DateTimeFormatter =
DateTimeFormatterBuilder()
.appendValue(YEAR, 4).appendLiteral('-').appendValue(MONTH_OF_YEAR, 2).appendLiteral('-').appendValue(DAY_OF_MONTH, 2)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2).appendLiteral(':').appendValue(MINUTE_OF_HOUR, 2).appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2)
.appendLiteral(' ')
.appendZoneId()
.toFormatter()
val aRfc3339DateString = "2016-03-17 03:24:38 Etc/GMT"
val parsedDate = ZonedDateTime.parse(aRfc3339DateString, appleRfc3339DateFormatter).toDate()
@hoangtranwork
Copy link
Author

hoangtranwork commented Oct 12, 2018

RFC3339 is so loose that it's better to make specialized parser for each vendor. In this case, Apple decides that their date should use space instead of T to separate the date and time part and no fraction seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment