java - 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"))
}
解决方案
正如 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,则它们将被完全省略。如果你不想要这个,你需要编写一个特殊情况。
推荐阅读
- regression - Bayesian Gamma regression, what is the correct link function?
- swift - 集合视图单元格标签覆盖其他单元格的标签
- python - python - 如何将折线图更改为条形图?
- sql - 如何在 Delphi 中正确翻译 sql 代码
- java - 如何在没有警告的情况下创建 JavaFX TableView?
- biztalk - BizTalk 映射 -- 仅翻译特定的发货至
- javascript - 获取异步函数中的上一项
- c# - 使用Graphics时图片内存不足,C#
- c# - 如何在多行字符串中转义引号?C#
- reactjs - react-native-svg onPress 事件不起作用