你可以创建自己的
DateTimeFormatter
:
val date1 = "2018-10-17T15:33:15 UTC"
val date2 = "2018-10-17T17:03:00 Europe/Prague"
val date3 = "2018-10-18T12:00:00 America/Kentucky/Monticello"
//with JDK
val formatter = java.time.format.DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendLiteral(' ')
.parseCaseSensitive()
.appendZoneRegionId()
.toFormatter();
println(ZonedDateTime.parse(date1, formatter))
println(ZonedDateTime.parse(date2, formatter))
println(ZonedDateTime.parse(date3, formatter))
//With Joda Time
val jodaFormatter = org.joda.time.format.DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss ZZZ").toFormatter()
println(jodaFormatter.parseDateTime(date1))
println(jodaFormatter.parseDateTime(date2))
println(jodaFormatter.parseDateTime(date3))
此格式化程序只能解析“europe/prague”,而不能解析“europe/praga”。您可以在此处找到所有支持的区域标识:
https://www.mkyong.com/java8/java-display-all-zoneid-and-its-utc-offset/