Java 8 Date Time: Convert from ISO 8601 String with TZ info, in this eg. UTC (javascript json friendly) to zone aware java date time object and render as local system date time
August 30, 2017 Leave a comment
Whole groovy sample (paste into the groovyconsole)
import java.time.ZonedDateTime import java.time.LocalDateTime import java.time.ZoneId import java.time.format.DateTimeFormatter final String UTC_DATE_TIME="2017-08-30T13:12:46.824Z" ZonedDateTime zdt = ZonedDateTime.parse(UTC_DATE_TIME,DateTimeFormatter.ISO_DATE_TIME) println zdt.getZone() // 'Z' => UTC println ZoneId.systemDefault() // "Europe/London" assert ZoneId.systemDefault().equals(ZonedDateTime.now().getZone()) // Defaults to JVM TZ //on windows, use tzutil.exe to get/set the system time zone //override for JVM with -Duser.timezone ZonedDateTime londonZdt = zdt.withZoneSameInstant(ZoneId.of("Europe/London")) ZonedDateTime rioZdt = zdt.withZoneSameInstant(ZoneId.of("America/Argentina/Buenos_Aires")) assert(!londonZdt.equals(rioZdt)) assert(!londonZdt.isBefore(rioZdt)) assert(!londonZdt.isAfter(rioZdt)) assert(londonZdt.isEqual(rioZdt)) println zdt //2017-08-30T13:12:46.824Z println londonZdt //2017-08-30T14:12:46.824+01:00[Europe/London] println rioZdt //2017-08-30T10:12:46.824-03:00[America/Argentina/Buenos_Aires] println "-----" println zdt.toLocalDateTime() //2017-08-30T13:12:46.824 println londonZdt.toLocalDateTime() //2017-08-30T14:12:46.824 println rioZdt.toLocalDateTime() //2017-08-30T10:12:46.824
output on my machine :
Z Europe/London 2017-08-30T13:12:46.824Z 2017-08-30T14:12:46.824+01:00[Europe/London] 2017-08-30T10:12:46.824-03:00[America/Argentina/Buenos_Aires] ----- 2017-08-30T13:12:46.824 2017-08-30T14:12:46.824 2017-08-30T10:12:46.824