简体   繁体   English

从 LocalDateTime.parse 缺少第二(00)

[英]missing second(00) from LocalDateTime.parse

missing second (00) from LocalDateTime.parse缺少LocalDateTime.parse第二个 (00)

LocalTime time = LocalTime.NOON;
DateTimeFormatter formatTime = DateTimeFormatter.ofPattern("HH:mm:ss");
String value ="20200810" + time.format(formatTime);
LocalDateTime localDateTime = LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"));

LOGS日志

=========value========== 2020081012:00:00
===localDateTime===2020-08-10T**12:00**

I tried to change LocalTime.NOON to LocalTime.of(12,0,0) too but still same result.我试图改变LocalTime.NOONLocalTime.of(12,0,0)太多,但还是同样的结果。

Write the following line into log:将以下行写入日志:

localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

The above line returns a string as per DateTimeFormatter.ISO_LOCAL_DATE_TIME .上面的行根据DateTimeFormatter.ISO_LOCAL_DATE_TIME返回一个字符串。

You can also specify a custom pattern as per your requirement eg您还可以根据您的要求指定自定义模式,例如

localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

or或者

localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHH:mm:ss"))

If you print localDateTime directly, it will print the string returned by toString method of LocalDateTime .如果直接打印localDateTime ,它会打印LocalDateTimetoString方法返回的字符串。

Note that since the second part in the time, 12:00:00 is 00 , the default toString implementation of LocalDateTime ignores the second part.请注意,由于时间中的second部分12:00:0000 ,因此LocalDateTime的默认toString实现会忽略second部分。

For your reference, given below is the toString() implementation of LocalDateTime :供您参考,下面给出的是LocalDateTimetoString()实现:

@Override
public String toString() {
    return date.toString() + 'T' + time.toString();
}

and given below the toString() implementation of LocalTime :并在下面给出LocalTimetoString()实现:

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(18);
    int hourValue = hour;
    int minuteValue = minute;
    int secondValue = second;
    int nanoValue = nano;
    buf.append(hourValue < 10 ? "0" : "").append(hourValue)
        .append(minuteValue < 10 ? ":0" : ":").append(minuteValue);
    if (secondValue > 0 || nanoValue > 0) {
        buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
        if (nanoValue > 0) {
            buf.append('.');
            if (nanoValue % 1000_000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
            } else if (nanoValue % 1000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
            } else {
                buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
            }
        }
    }
    return buf.toString();
}

As you can see, the second and nano parts are included only when their values are greater than 0 .如您所见,仅当second部分和nano部分的值大于0时才包含它们。

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

相关问题 LocalDateTime - 使用 LocalDateTime.parse 进行反序列化 - LocalDateTime - deserialization with LocalDateTime.parse 如何使用 LocalDateTime.parse(...) 解析 ISO 日期 - How to parse an ISO date with LocalDateTime.parse(...) Java DateTimeFormatter 和 LocalDateTime.parse 麻烦 - Java DateTimeFormatter and LocalDateTime.parse trouble Java LocalDateTime.parse 具有毫秒精度但可选微秒精度 - Java LocalDateTime.parse with millisecond precision but optional microsecond precision LocalDateTime.parse处的java.time.format.DateTimeParseException - java.time.format.DateTimeParseException at LocalDateTime.parse LocalDateTime.parse(yyyMMddHHmmssSSS, DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS") 错误 - LocalDateTime.parse(yyyMMddHHmmssSSS, DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS") error Java 使用 LocalDateTime 0000-00-00 00:00:00 解析 JSON - Java Parse JSON with LocalDateTime 0000-00-00 00:00:00 如何在返回类型为 LocalDateTime 的方法中从 LocalDateTime 对象中获取第二个? - How to get the second from LocalDateTime objects in a method with return type LocalDateTime? 如何将“2016 年 11 月 20 日 12:00:00 AM”解析为 LocalDateTime? - How do I parse “Nov 20, 2016 12:00:00 AM” to LocalDateTime? 用leap秒来解析持续时间,例如00:00:60 - Parse time duration with leap second like 00:00:60
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM