ZonedDateTime 解析异常
ZonedDateTime parse exception
我正在尝试将字符串转换为 ZonedDateTime。
我试过以下方法:
SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
zonedDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long timeMs = zonedDateTimeFormat.parse("2017-07-18T20:26:28.582+03:00[Asia/Istanbul]").getTime();
给出java.text.ParseException: Unparseable date
如何将以下字符串解析为 ZonedDateTime
2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
ZonedDateTime.parse
似乎旨在处理您提供的确切字符串。没有必要通过旧的SimpleDateFormat
对于 ZonedDateTime,我们需要使用 ZonedDateTime.parse
方法和 DateTimeFormatter
。如果我没记错的话你有一个 ISO
日期:
ZonedDateTime zonedDateTime = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
DateTimeFormatter.ISO_DATE_TIME
);
System.out.println(zonedDateTime); //2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
您可以使用 ISO_ZONED_DATE_TIME
or ISO_DATE_TIME
。两者都能够解析带有偏移量和区域的日期时间。
java.time
API 有许多内置格式可以简化解析和格式化过程。您尝试解析的字符串采用标准 ISO_ZONED_DATE_TIME 格式。因此,您可以通过以下方式轻松解析它,然后从纪元中获取毫秒数:
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
formatter); // prints 2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
long timeInMs = zdt.toInstant().toEpochMilli();
我正在尝试将字符串转换为 ZonedDateTime。
我试过以下方法:
SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
zonedDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long timeMs = zonedDateTimeFormat.parse("2017-07-18T20:26:28.582+03:00[Asia/Istanbul]").getTime();
给出java.text.ParseException: Unparseable date
如何将以下字符串解析为 ZonedDateTime
2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
ZonedDateTime.parse
似乎旨在处理您提供的确切字符串。没有必要通过旧的SimpleDateFormat
对于 ZonedDateTime,我们需要使用 ZonedDateTime.parse
方法和 DateTimeFormatter
。如果我没记错的话你有一个 ISO
日期:
ZonedDateTime zonedDateTime = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
DateTimeFormatter.ISO_DATE_TIME
);
System.out.println(zonedDateTime); //2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
您可以使用 ISO_ZONED_DATE_TIME
or ISO_DATE_TIME
。两者都能够解析带有偏移量和区域的日期时间。
java.time
API 有许多内置格式可以简化解析和格式化过程。您尝试解析的字符串采用标准 ISO_ZONED_DATE_TIME 格式。因此,您可以通过以下方式轻松解析它,然后从纪元中获取毫秒数:
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
formatter); // prints 2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
long timeInMs = zdt.toInstant().toEpochMilli();