简体   繁体   English

Java 8 LocalDateTime 在解析具有 00 秒的日期字符串值(如“2018-07-06 00:00:00”)时丢弃 00 秒值

[英]Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 00:00:00"

The code (Java 8) snippet below drops the seconds part of my date time when the seconds value is zero within the date parsed using LocalDateTime.parse , like 2018-07-10 00:00: 00 :下面的代码(Java 8)片段在使用LocalDateTime.parse解析的日期内秒值为零时删除我的日期时间秒部分,例如 2018-07-10 00:00: 00

final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime localDateTime = LocalDateTime.parse("2018-07-06 00:00:00", dateTimeFormatter);
final String lexicalDate = localDateTime.toString();
System.out.println("Lexical Date : "+ lexicalDate);
final XMLGregorianCalendar gregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate);
System.out.println("Gregorian Calendar : "+ gregorianCalendar);

The Lexical Date is printed as :词法日期打印为:

Lexical Date : 2018-07-10T00:00词法日期:2018-07-10T00:00

instead of :代替 :

Lexical Date : 2018-07-10T00:00:00词法日期:2018-07-10T00:00:00

Now this is affecting the date value of the gregorian calendar which returns null when the second is dropped.现在这会影响公历的日期值,当第二个被删除时它返回 null Other cases when the seconds value is greater than Zero, it works perfectly.秒值大于零的其他情况下,它可以完美运行。

javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(lexicalDate)

The above code is returning null whenever the seconds value is dropped because of 00 seconds value within the parsed string.每当由于解析字符串中的 00 秒值而丢弃秒值时,上面的代码都会返回 null。

Can someone assist with a better way that handles this issue using LocalDate time, otherwise it might be a bug/funny control in Java 8 LocalDateTime.有人可以协助使用 LocalDate 时间处理此问题的更好方法,否则它可能是 Java 8 LocalDateTime 中的错误/有趣的控件。

Please note I do not have control over this date value, it's coming from a third party platform.请注意,我无法控制此日期值,它来自第三方平台。

Feature, not a bug功能,而不是错误

You are seeing the documented behavior of the particular DateTimeFormatter used by the LocalDateTime::toString method.您正在看到LocalDateTime::toString方法使用的特定DateTimeFormatter的记录行为。

Excerpt, my emphasis:摘录,我的重点:

The output will be one of the following ISO-8601 formats:输出将是以下ISO-8601格式之一:

uuuu-MM-dd'T'HH:mm uuuu-MM-dd'T'HH:mm

uuuu-MM-dd'T'HH:mm:ss uuuu-MM-dd'T'HH:mm:ss

uuuu-MM-dd'T'HH:mm:ss.SSS uuuu-MM-dd'T'HH:mm:ss.SSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSS uuuu-MM-dd'T'HH:mm:ss.SSSSSS

uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero .使用格式将是输出完整时间值的最短格式,其中省略部分隐含为零

If you want other behavior when generating a String to represent the value of you LocalDateTime , use a different DateTimeFormatter and pass it to LocalDateTime::format .如果在生成 String 时需要其他行为来表示LocalDateTime的值,请使用不同的DateTimeFormatter并将其传递给LocalDateTime::format

String output = myLocalDateTime.format( someOtherFormatter ) ;

The LocalDateTime has no “format” as it is not text. LocalDateTime没有“格式”,因为它不是文本。 It is the job of the DateTimeFormatter to parse or generate String objects of a particular format. DateTimeFormatter的工作是解析或生成特定格式的String对象。

您需要使用正确的格式化程序格式化您的日期,而不是通过调用toString()使用默认的格式化程序。

final String lexicalDate = localDateTime.format(dateTimeFormatter);

您可以使用以下模式来实现所需的格式:

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");

DateTimeFormatter removes 00 from date after parsing, you just have to reformat it again DateTimeFormatter 解析后从日期中删除 00,你只需要重新格式化它

please refer below code snippet:请参考以下代码片段:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String currentTime= "2017-10-19 22:00:00";
LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);
//2017-10-19T22:00
String formattedDate=datetime.format(formatter);
//2017-10-19 22:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 日期转换从12:00:00到00:00:00 java - Date conversion from 12:00:00 to 00:00:00 java 解析日期字符串,格式为[2012-07-15T20:55:33 + 00:00] - Parsing date string in Java of format [2012-07-15T20:55:33+00:00] DateTimeFormatter 2018-06-18 20:07:08.908193 + 00 - DateTimeFormatter 2018-06-18 20:07:08.908193+00 Java 使用 LocalDateTime 0000-00-00 00:00:00 解析 JSON - Java Parse JSON with LocalDateTime 0000-00-00 00:00:00 日期功能正在修剪秒,其中秒是00 - Date Function is trimming seconds where seconds is 00 以00:00格式创建递增计时器,以秒为单位? - Create an incrementing timer in seconds in 00:00 format? 使用 Instant.parse 解析 2018-05-01T00:00:00 日期时出错 - Error while parsing 2018-05-01T00:00:00 date using Instant.parse java.time.Instant 无法从像 '2016-07-02T00:00:00Z' 这样的字符串解析 - java.time.Instant could not be parsed from string like '2016-07-02T00:00:00Z' Java Localdatetime 解析设置偏移量 +5:30 而我希望偏移量为 00:00:00 - Java Localdatetime parsing setting offset +5:30 whereas I want offset to be 00:00:00 得到例如“无法将值转换为00:00:00”从第12列到TIMESTAMP的异常“ - Getting the exception like “Cannot convert value '0000-00-00 00:00:00' from column 12 to TIMESTAMP”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM