简体   繁体   English

Java时代以来的时代

[英]Java time since the epoch

In Java, how can I print out the time since the epoch given in seconds and nanoseconds in the following format : 在Java中,如何以秒和纳秒的形式打印出自上述时间以来的时间,格式如下:

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

My input is: 我的意见是:

long mnSeconds;
long mnNanoseconds;

Where the total of the two is the elapsed time since the epoch 1970-01-01 00:00:00.0 . 其中两者的总和是自1970-01-01 00:00:00.0纪元以来经过的时间。

Use this and divide by 1000 使用它并除以1000

long epoch = System.currentTimeMillis();

System.out.println("Epoch : " + (epoch / 1000));

You can do this 你可以这样做

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

eg 例如

2012-08-08 19:52:21.123456789

if you don't really need any more than milliseconds you can do 如果你真的不需要超过毫秒,你可以做

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

tl;dr 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.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the old troublesome date-time classes such as java.util.Date , .Calendar , java.text.SimpleDateFormat , java.sql.Date , and more. 这些类取代了旧的麻烦的日期时间类,如java.util.Date.Calendarjava.text.SimpleDateFormatjava.sql.Date等。 The Joda-Time team also advises migration to java.time. Joda-Time团队还建议迁移到java.time。

Instant

The Instant class represents a moment on the timeline in UTC with a resolution up to nanoseconds. Instant类表示UTC时间轴上的一个时刻,分辨率高达纳秒。

long mnSeconds = … ;
long mnNanoseconds = … ;

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

Or pass both numbers to the of , as two arguments. 或者将两个数字都传递给of ,作为两个参数。 Different syntax, same result. 语法不同,结果相同。

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

To get a String representing this date-time value, call Instant::toString . 要获取表示此日期时间值的String,请调用Instant::toString

String output = instant.toString();

You will get a value such as 2011-12-03T10:15:30.987654321Z , standard ISO 8601 format. 您将获得一个值,例如2011-12-03T10:15:30.987654321Z ,标准ISO 8601格式。 Replace the T with a SPACE if you wish. 如果您愿意,可以用空格替换T For other formats, search Stack Overflow to learn about DateTimeFormatter . 对于其他格式,请搜索Stack Overflow以了解DateTimeFormatter


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

java.util.Date class has a constructor which accepts the epoch milliSeconds. java.util.Date类有一个接受epoch milliSeconds的构造函数。

Check the java doc and try to make use of it. 检查java doc并尝试使用它。

It depends a bit on the values of you mnSeconds and mnNanoseconds but all you need to do with a formatter like that one (which has millisecond precision) is to create a java.util.Date. 它取决于你的mnSeconds和mnNanoseconds的值,但你需要做的就是那个格式化程序(具有毫秒精度),就是创建一个java.util.Date。 If mnNanoseconds is the number of nanoseconds on top of your mnSeconds, I would assume it to be something like 如果mnNanoseconds是mnSeconds之上的纳秒数,我会认为它类似于

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

Then it is a matter of formatting it with your formatter before printing it. 然后在打印之前用格式化程序格式化它。

You can use 您可以使用

new java.util.Date(mnSeconds);

and then SimpleDateFormat to format your output. 然后使用SimpleDateFormat格式化输出。

Nanoseconds are not supported by Date. Date不支持纳秒。 You have to manually add Nanoseconds or use some framework (is there one?). 你必须手动添加纳秒或使用一些框架(有一个吗?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM