【问题标题】:missing second(00) from LocalDateTime.parseLocalDateTime.parse 中缺少秒 (00)
【发布时间】:2020-12-15 05:26:40
【问题描述】:

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),但结果仍然相同。

【问题讨论】:

  • 如何创建“===localDateTime===2020-08-10T12:00”的输出?问题就在那里。
  • 你是打印localDateTime还是打印localDateTime.format(formatTime)
  • 是 LocalDateTime.parse() 之后的问题。我将 system.out.println 放入代码并获取日志。 System.out.println("===localDateTime===" +localDateTime);
  • 这就是原因...默认格式化程序或toString() 会截断零秒...
  • 如果你只是 System.out.println(localDateTime) 它将调用 localDateTime.toString() 的默认实现,这将返回 date.toString() + 'T' + time.toString() 并且 time.toString() JavaDoc 状态:“使用的格式将是最短的输出时间的完整值,其中省略的部分被暗示为零。"

标签: java java-time localdatetime datetimeformatter


【解决方案1】:

将以下行写入日志:

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:0000LocalDateTime的默认toString实现忽略second部分。

供您参考,下面给出的是LocalDateTimetoString() 实现:

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

并在下面给出toString()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();
}

如您所见,secondnano 部分仅在其值大于 0 时才包含在内。

【讨论】:

  • 谢谢阿文德。有没有办法可以在解析后添加 00 秒?
  • @user84 - 推荐的方法是获取由DateTimeFormatter 格式化的字符串,如我的回答中所述。另一种方法是覆盖LocalDateTimetoString 实现,我强烈建议您不要这样做。
  • @user84 您的 LocalDateTime 没有任何遗漏。它具有您想要并期望它具有的完全相同的值。您的问题在于您用于日志输出的所述对象的字符串表示形式。如果您希望 LocalDateTime 对象的输出采用特定格式,那么您需要使用具有您想要输出格式的格式化程序。
  • 谢谢大家,根据建议在再次格式化后获得秒数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多