简体   繁体   English

将日期从ISO 8601 Zulu字符串转换为Java 8中的java.time.Instant

[英]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字符串到日期的转换 ,但不起作用。

tl;dr TL;博士

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" )

ISO 8601 ISO 8601

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" ) ;

About java.time 关于java.time

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.DateCalendarSimpleDateFormat

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 . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

Don't build your own formatter 不要构建自己的格式化程序

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日

What went wrong in your code? 您的代码出了什么问题?

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). 格式模式字母区分大小写(您也可以在链接到的问题的第一个答案中的表中看到这一点)。

  • You used uppercase 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一年。
  • Uppercase DD is for day of year. 大写DD是一年中的一天。 For day of month you need lowercase dd . 对于每月的某一天,您需要小写dd
  • Lowercase 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.

相关问题 如何在 Spring Webflux 中将 java.time.Instant 序列化为 ISO 字符串 - How to serialize java.time.Instant as ISO string in Spring Webflux java.time.即时响应 JavaScript 日期 - java.time.Instant response to JavaScript date 获取错误:日期中的from(java.time.Instant)无法应用于(org.threeten.bp.instant) - Getting error: from(java.time.Instant) in Date cannot be applied to (org.threeten.bp.instant) 使用 Jackson ObjectMapper 在 java.time.Instant 和 java.util.Date 之间进行转换 - Use Jackson ObjectMapper to convert between java.time.Instant and java.util.Date 根据 Java 中的 State 名称将 ISO8601 日期时间转换为另一种 ISO8601 格式? - convert ISO8601 date time to another ISO8601 format based on State name in Java? 如何在 Java 中将 UTC 日期时间转换为 ISO 8601 格式? - how to convert UTC date-time into ISO 8601 format in Java? Java将ISO 8601字符串转换为忽略偏移量的日期 - Java convert ISO 8601 string to Date ignoring offset Java-将字符串转换为ISO 8601日期格式错误Joda-Time - Java - Convert string to ISO 8601 date format error Joda-Time 同时将XMLGregorianCalendar转换为java.time.Instant +切换时区 - Convert XMLGregorianCalendar to java.time.Instant + switch timezone in the meanwhile 在Java中,是否可以将日期和时间转换为没有时区的ISO 8601格式的字符串? - In Java, Can I Convert A Date and Time To An ISO 8601 formatted String Without A Timezone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM