Java 时代以来的时间

IT小君   2021-12-09T03:25:47

在 Java 中,如何以以下格式以秒和纳秒为单位打印自纪元以来的时间:

java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

我的输入是:

long mnSeconds;
long mnNanoseconds;

其中两者的总和是自 epoch 以来经过的时间1970-01-01 00:00:00.0

点击广告,支持我们为你提供更好的服务
评论(6)
IT小君

使用它并除以 1000

long epoch = System.currentTimeMillis();

System.out.println("Epoch : " + (epoch / 1000));
2021-12-09T03:25:59   回复
IT小君

tl;博士

    Instant                        // Represent a moment in UTC.
    .ofEpochSecond( mnSeconds )   // Determine a moment from a count of whole seconds since the Unix epoch of the first moment of 1970 in UTC (1970-01-01T00:00Z). 
    .plusNanos( mnNanoseconds )    // Add on a fractional second as a count of nanoseconds. Returns another `Instant` object, per Immutable Objects pattern.
    .toString()                    // Generate text representing this `Instant` object in standard ISO 8601 format.
    .replace( "T" , " " )          // Replace the `T` in the middle with a SPACE. 
    .replace "Z" , "" )            // Remove the `Z` on the end (indicating UTC).

时间

java.time框架是建立在Java 8和更高版本。这些类取代旧的麻烦日期时间类,如java.util.Date.Calendarjava.text.SimpleDateFormatjava.sql.Date,等等。乔达时球队还建议迁移java.time。

Instant

Instant类表示UTC时间线,分辨率高达纳秒了一下。

long mnSeconds = … ;
long mnNanoseconds = … ;

Instant instant = Instant.ofEpochSecond( mnSeconds ).plusNanos( mnNanoseconds );

或者将两个数字of作为两个参数传递给, 。不同的语法,相同的结果。

Instant instant = Instant.ofEpochSecond( mnSeconds , mnNanoseconds );

要获取表示此日期时间值的字符串,请调用Instant::toString.

String output = instant.toString();

您将获得一个值,例如2011-12-03T10:15:30.987654321Z标准ISO 8601格式。T如果您愿意,请将 替换为空格。对于其他格式,搜索 Stack Overflow 以了解DateTimeFormatter.


关于java.time

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

现在处于维护模式Joda-Time项目建议迁移到java.time类。

要了解更多信息,请参阅Oracle 教程并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

您可以直接与数据库交换java.time对象。使用符合JDBC 4.2或更高版本JDBC 驱动程序不需要字符串,不需要类。java.sql.*

从哪里获得 java.time 类?

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多

2021-12-09T03:26:03   回复
IT小君

你可以这样做

public static String format(long mnSeconds, long mnNanoseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.");
    return sdf.format(new Date(mnSeconds*1000))
           + String.format("%09d", mnNanoseconds);
}

例如

2012-08-08 19:52:21.123456789

如果你真的不需要任何超过毫秒的时间,你可以做

public static String format(long mnSeconds, long mnNanoseconds) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    return sdf.format(new Date(mnSeconds*1000 + mnNanoseconds/1000000));
}
2021-12-09T03:26:07   回复
IT小君

这在一定程度上取决于您的 mnSeconds 和 mnNanoseconds 的值,但是您需要使用像这样的格式化程序(具有毫秒精度)来创建一个 java.util.Date。如果 mnNanoseconds 是 mnSeconds 之上的纳秒数,我会假设它类似于

Date d = new Date(mnSeconds*1000+mnNanosecods/1000000)

然后是在打印之前使用格式化程序对其进行格式化的问题。

2021-12-09T03:26:07   回复
IT小君

java.util.Date 类有一个接受纪元毫秒的构造函数。

检查java 文档并尝试使用它。

2021-12-09T03:26:07   回复
IT小君

您可以使用

new java.util.Date(mnSeconds);

然后SimpleDateFormat格式化您的输出。

Date 不支持纳秒。您必须手动添加纳秒或使用一些框架(有吗?)。

2021-12-09T03:26:08   回复