Discuss / Java / 针对“本月第一天0:00时刻:” LocalDateTime 秒是0 的话 格式化只到分钟

针对“本月第一天0:00时刻:” LocalDateTime 秒是0 的话 格式化只到分钟

Topic source

何霖

#1 Created at ... [Delete] [Delete and Lock User]

eg: 

2022-12-01T00:00

是由于LocalTime toString() 方法的判断是这样的:

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

        }

    }

}


  • 1

Reply