[英]Convert Date from ISO 8601 Zulu string to java.time.Instant in Java 8
I want to convert string date format into java.time.Instant 我想将字符串日期格式转换为java.time.Instant
I am getting exception while parsing date. 我在解析日期时遇到异常。
java.lang.IllegalArgumentException: Too many pattern letters: s
I am using below code for conversion first from String to date. 我使用下面的代码进行转换,从String到date。
String string = "2018-07-17T09:59:51.312Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD'T'hh:mm:ss.sssZ", Locale.FRANCE);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date);
I want to convert "timestamp": "2018-07-17T09:59:51.312Z"
in format time in the ISO 8601 format YYYY-MM-DDThh:mm:ss.sssZ
in the UTC timezone. 我想在UTC时区中以ISO 8601格式YYYY-MM-DDThh:mm:ss.sssZ
格式时间转换“timestamp”: "2018-07-17T09:59:51.312Z"
。
Checked Java string to date conversion , but not working. 检查Java字符串到日期的转换 ,但不起作用。
convert string date format into java.time.Instant 将字符串日期格式转换为java.time.Instant
Skip the formatting pattern. 跳过格式化模式。 Just parse. 只是解析。
Instant.parse( "2018-07-17T09:59:51.312Z" )
Yes, you used incorrect formatting pattern as indicated in the first Answer . 是的,您使用了错误的格式设置模式,如第一个答案中所示 。
But you needn't specify a formatting pattern at all. 但是您根本不需要指定格式化模式 。 Your input string is in standard ISO 8601 format. 您的输入字符串采用标准ISO 8601格式。 The java.time classes use ISO 8601 formats by default when parsing/generating strings. 在解析/生成字符串时, java.time类默认使用ISO 8601格式。
The Z
on the end means UTC , and is pronounced “Zulu”. 结尾的Z
表示UTC ,发音为“Zulu”。
Instant instant = Instant.parse( "2018-07-17T09:59:51.312Z" ) ;
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧遗留日期时间类,如java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 。 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310 。
You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 。 No need for strings, no need for java.sql.*
classes. 不需要字符串,不需要java.sql.*
类。
Where to obtain the java.time classes? 从哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多 。
Don't struggle with writing your own format pattern string. 不要为编写自己的格式模式字符串而烦恼。 Your string is in an ISO 8601 format that is built into java.time. 您的字符串采用ISO 8601格式,内置于java.time中。 To parse into a java.time.Instant
(as your title says): 解析为java.time.Instant
(如标题所示):
String string = "2018-07-17T09:59:51.312Z";
Instant inst = Instant.parse(string);
System.out.println(inst);
Output: 输出:
2018-07-17T09:59:51.312Z 2018-07-17T09:59:51.312Z
To parse into a LocalDate
(as your code snippet seems to intend): 要解析为LocalDate
(正如您的代码片段似乎意图):
LocalDate date = LocalDate.parse(string, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
System.out.println(date);
2018-07-17 2018年7月17日
Format pattern letters are case sensitive (you can also see this from the tables in the first answer to the question you are linking to). 格式模式字母区分大小写(您也可以在链接到的问题的第一个答案中的表中看到这一点)。
YYYY
for year. 你使用大写YYYY
一年。 Uppercase Y
is for week year, only useful with a week number. 大写Y
表示一周,仅适用于周数。 Use either uuuu
or lowercase yyyy
for year. 使用uuuu
或小写yyyy
一年。 DD
is for day of year. 大写DD
是一年中的一天。 For day of month you need lowercase dd
. 对于每月的某一天,您需要小写dd
。 hh
is for hour with AM or PM from 01 through 12 and only useful with an AM/PM marker. 小写hh
表示小时,AM或PM从01到12,仅适用于AM / PM标记。 You need uppercase HH
for hour of day from 00 through 23. 从00到23,您需要大写的HH
。 Finally you correctly used lowercase ss
for seconds, but also sss
for fraction of second. 最后你正确地使用小写ss
秒,但sss
也只是秒的一小部分。 For the latter you need uppercase SSS
. 对于后者,您需要大写的SSS
。 This error is the reason for your error message: Since seconds only go up to 60 (including a leap second), three s
as in sss
does not make sense, and DateTimeFormatter
objects to this. 此错误是您的错误消息的原因:由于秒数最多只有60(包括闰秒),因此sss
中的三个s
没有意义,而DateTimeFormatter
对此反对。 From the documentation: 从文档:
Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified. 最多可以指定两个字母'd','H','h','K','k','m'和's'。
Link: DateTimeFormatter
documentation 链接: DateTimeFormatter
文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.