[英]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.NOON
到LocalTime.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
,它会打印LocalDateTime
的toString
方法返回的字符串。
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:00
是00
,因此LocalDateTime
的默认toString
实现会忽略second
部分。
For your reference, given below is the toString()
implementation of LocalDateTime
:供您参考,下面给出的是
LocalDateTime
的toString()
实现:
@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}
and given below the toString()
implementation of LocalTime
:并在下面给出
LocalTime
的toString()
实现:
@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.