如何使用默认区域解析ZonedDateTime?
如何从不包含的字符串和其他字段进行解析?ZoneDateTime
zone
以下是斯波克的重现测试:
import spock.lang.Specification
import spock.lang.Unroll
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
@Unroll
class ZonedDateTimeParsingSpec extends Specification {
def "DateTimeFormatter.ISO_ZONED_DATE_TIME parsing incomplete date: #value #expected"() {
expect:
ZonedDateTime.parse(value, DateTimeFormatter.ISO_ZONED_DATE_TIME) == expected
where:
value | expected
'2014-04-23T04:30:45.123Z' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.UTC)
'2014-04-23T04:30:45.123+01:00' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneOffset.ofHours(1))
'2014-04-23T04:30:45.123' | ZonedDateTime.of(2014, 4, 23, 4, 30, 45, 123_000_000, ZoneId.systemDefault())
'2014-04-23T04:30' | ZonedDateTime.of(2014, 4, 23, 4, 30, 0, 0, ZoneId.systemDefault())
'2014-04-23' | ZonedDateTime.of(2014, 4, 23, 0, 0, 0, 0, ZoneId.systemDefault())
}
}
前两个测试通过,所有其他测试都失败了 DateTimeParseException:
- '2014-04-23T04:30:45.123' 无法在索引 23 处解析
- '2014-04-23T04:30' 无法在索引 16 处解析
- '2014-04-23' 无法在索引 10 处解析
如何解析将时间和区域设置为默认值的不完整日期?