英文:
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,它将打印 LocalDateTime 的 toString 方法返回的字符串。
请注意,由于时间中的 second 部分在 12:00:00 中是 00,因此 LocalDateTime 的默认 toString 实现会忽略 second 部分。
供您参考,以下是 LocalDateTime 的 toString() 实现:
@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}
以下是 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();
}
如您所见,仅当 second 和 nano 部分的值大于 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("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.
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() + 'T' + 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 < 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论