缺少秒数(00),来自于 LocalDateTime.parse。

huangapple go评论79阅读模式
英文:

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

日志

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

我还尝试将 LocalTime.NOON 更改为 LocalTime.of(12, 0, 0),但结果仍然相同。

英文:

missing second (00) from LocalDateTime.parse

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.

答案1

得分: 5

将以下行写入日志:

localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)

上述行根据 DateTimeFormatter.ISO_LOCAL_DATE_TIME 返回一个字符串。

您还可以根据需要指定自定义模式,例如:

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

或者

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

如果直接打印 localDateTime,它将打印 LocalDateTimetoString 方法返回的字符串。

请注意,由于时间中的 second 部分在 12:00:00 中是 00,因此 LocalDateTime 的默认 toString 实现会忽略 second 部分。

供您参考,以下是 LocalDateTimetoString() 实现:

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

以下是 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();
}

如您所见,仅当 secondnano 部分的值大于 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.

You can also specify a custom pattern as per your requirement e.g.

localDateTime.format(DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd HH:mm:ss&quot;))

or

localDateTime.format(DateTimeFormatter.ofPattern(&quot;yyyyMMddHH:mm:ss&quot;))

If you print localDateTime directly, it will print the string returned by toString method of LocalDateTime.

Note that since the second part in the time, 12:00:00 is 00, the default toString implementation of LocalDateTime ignores the second part.

For your reference, given below is the toString() implementation of LocalDateTime:

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

and given below the toString() implementation of LocalTime:

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(18);
    int hourValue = hour;
    int minuteValue = minute;
    int secondValue = second;
    int nanoValue = nano;
    buf.append(hourValue &lt; 10 ? &quot;0&quot; : &quot;&quot;).append(hourValue)
        .append(minuteValue &lt; 10 ? &quot;:0&quot; : &quot;:&quot;).append(minuteValue);
    if (secondValue &gt; 0 || nanoValue &gt; 0) {
        buf.append(secondValue &lt; 10 ? &quot;:0&quot; : &quot;:&quot;).append(secondValue);
        if (nanoValue &gt; 0) {
            buf.append(&#39;.&#39;);
            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.

huangapple
  • 本文由 发表于 2020年8月26日 19:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63597175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定