首页 > 解决方案 > DateTime 格式以具有尾随零并捕捉到毫、微或纳米格式

问题描述

我有一些这样的代码

import java.time.Instant


val tstamp = 1546344620.1877
val nanoAdj = ((BigDecimal(tstamp) - tstamp.toLong) * 1000000000).toLong
Instant.ofEpochSecond(tstamp.toLong, nanoAdj).toString
// will print : 2019-01-01T12:10:20.187700Z

toString从 Instant 对象创建的 from非常ofEpochSecond适合尾随零到毫/微/纳米组,但我正在努力让格式化来做同样的事情。我需要将格式更改为类似2019-01-01 12:10:20.187700 UTC

其他示例:

2019-01-01 12:10:20 UTC // no fractions
2019-01-01 12:10:20.180 UTC // milliseconds
2019-01-01 12:10:20.187700 UTC // microseconds
2019-01-01 12:10:20.187738200 UTC // nanoseconds

我正在使用如下所示的 DateTimeFormatter,但我愿意接受其他建议。

def formatter: DateTimeFormatter = {
      val tz_format = new DateTimeFormatterBuilder()
        .optionalStart
        .parseCaseSensitive()
        .appendZoneRegionId()
        .toFormatter

      val datetime_format = new DateTimeFormatterBuilder()
        .appendValue(HOUR_OF_DAY, 2)
        .appendLiteral(':')
        .appendValue(MINUTE_OF_HOUR, 2)
        .optionalStart()
        .appendLiteral(':')
        .appendValue(SECOND_OF_MINUTE, 2)
        .optionalStart()
        .appendFraction(NANO_OF_SECOND, 0, 9, true)
        .toFormatter

      new DateTimeFormatterBuilder()
        .parseCaseInsensitive
        .append(ISO_LOCAL_DATE)
        .appendLiteral(' ')
        .append(datetime_format)
        .appendLiteral(' ')
        .append(tz_format)
        .toFormatter
        .withZone(ZoneId.of("UTC"))
    }

标签: javascala

解决方案


正如 Andreas 在评论中所说,格式化类DateTimeFormatter, 不支持这个漂亮的功能。我最好的建议是利用LocalTime.toString()它,它有:

private static final DateTimeFormatter dateFormatter
        = DateTimeFormatter.ofPattern("uuuu-MM-dd ");

private static String formatEpochSeconds(String epochSeconds) {
    BigDecimal totalSeconds = new BigDecimal(epochSeconds);
    long seconds = totalSeconds.longValue();
    long nanos = totalSeconds.subtract(new BigDecimal(seconds))
            .multiply(new BigDecimal(TimeUnit.SECONDS.toNanos(1)))
            .longValue();
    OffsetDateTime dateTime = Instant.ofEpochSecond(seconds, nanos)
            .atOffset(ZoneOffset.UTC);
    return dateTime.format(dateFormatter)
            + dateTime.toLocalTime()
            + " UTC";
}

尝试一下:

System.out.println(formatEpochSeconds("1546344600.0"));
System.out.println(formatEpochSeconds("1546344620.0"));
System.out.println(formatEpochSeconds("1546344620.18"));
System.out.println(formatEpochSeconds("1546344620.1877"));
System.out.println(formatEpochSeconds("1546344620.1877382"));

输出是:

2019-01-01 12:10 UTC
2019-01-01 12:10:20 UTC
2019-01-01 12:10:20.180 UTC
2019-01-01 12:10:20.187700 UTC
2019-01-01 12:10:20.187738200 UTC

请注意,如果秒数为 0.0,则它们将被完全省略。如果你不想要这个,你需要编写一个特殊情况。


推荐阅读