当前位置: 首页 > 知识库问答 >
问题:

Java将19位Unix时间戳转换为可读日期

丰誉
2023-03-14

我正在尝试将19位Unix时间戳(如155843950471000000(15亿分之一)转换为可读的日期/时间格式。我的时间戳以6个零结束,这表明时间以纳秒为单位。

我遇到过一些例子,人们使用了我不需要的时区。另一个例子是这样使用ofEpochSecond:

Instant instant = Instant.ofEpochSecond(seconds, nanos);

但是我不确定我是否需要使用ofEpoch秒钟。

下面的代码给出了我实现这一目标的最新方法:

String timeStamp = "1558439504711000000";
long unixNanoSeconds = Long.parseLong(timeStamp);
Date date = new java.util.Date(timeStamp*1000L); 
// My preferred date format
SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss");                    
String formattedDate = sdf.format(date);
System.out.println("The timestamp in your preferred format is: " +  formattedDate); 

但是我得到的输出是这样的:

// The timestamp in your preferred format is: 11-12-49386951 11:43:20

它没有以例如2019格式显示年份格式。

共有3个答案

曹智
2023-03-14

试着用这个

Date date = new java.util.Date(timeStamp/1000000); 

不是乘以1000,而是除以1000000

通建安
2023-03-14

我认为这没有错,你处理的是一个代表未来日期(一个非常遥远的未来日期)的时间戳。

如果你考虑这一点:

字符串timeStamp="1558439504";

这应该给你: 05/21/2019 @ 11:51am (UTC)

我认为有一个简单的方法来确定日期。只需首先基于时间戳创建瞬间,然后执行以下操作:

Date myDate = Date.from(instant);
虞裕
2023-03-14

切勿在java.util.Date中使用遗留类。相反,使用现代的java.time.Instant

Instant                                  // The modern way to represent a moment in UTC with a resolution of nanoseconds. Supplants the terrible `java.util.Date` class.
.ofEpochSecond(                          // Parse a count since epoch reference of 1970-01-01T00:00:00Z.
    0L ,                                 // Passing zero for the count of whole seconds, to let the class determine this number from the 2nd argument.
    Long.parse( "1558439504711000000" )  // Count of nanoseconds since the epoch reference of 1970-01-01T00:00:00Z. 
)                                        // Returns a `Instant` object.
.atZone(                                 // Adjust from UTC to the wall-clock time used by the people of a specific region (a time zone).
    ZoneId.of( "Europe/London" ) 
)                                        // Returns a `ZonedDateTime` object. Same moment as the `Instant`, same point on the timeline, different wall-clock time.
.format(                                 // Generate text to communicate the value of the moment as seen through this time zone.
    DateTimeFormatter.ofPattern(         // Define how to format our generated text.
        "dd-MM-uuuu HH:mm:ss" ,          // Specify your desired formatting pattern.
        Locale.UK                        // Pass a `Locale` to be used in localizing, to (a) determine human language used in translating name of day-of-week and such, and (b) determine cultural norms to decide issues of capitalization, abbreviation, etc. Not really needed for this particular formatting pattern, but a good habit to specify `Locale`.
    )                                    // Returns a `DateTimeFormatter` object.
)                                        // Returns a `String` object containing our text.

21-05-2019 12:51:44

…或者…

Instant
.ofEpochSecond (
    TimeUnit.NANOSECONDS.toSeconds( 
       Long.parse( "1558439504711000000" ) 
    ) ,
    ( 1_558_439_504_711_000_000L % 1_000_000_000L )
)
.toString()

2019-05-21T10:41:44.711Z

请注意小时差,因为时区比 UTC 早一小时。

java.util。日期类是可怕的。连同它的同龄人,如日历

A<code>java.util。Date对象表示UTC中的一个时刻,分辨率为毫秒。它的替代品是<code>java.time。瞬间,也是UTC中的瞬间,但分辨率为纳秒。在内部,两者都跟踪自UTC 1970年第一时刻的历元参考以来的计数。

为了避免处理庞大的数字,在内部,瞬间跟踪1970年以来的整秒数加上以纳秒为单位的小数秒数。两个独立的数字。这些是您需要为Instant.ofepochssecond提供的内容。

使用 Long 类将输入字符串解析字符串。顺便说一句,请注意,您的值正在接近64位整数的极限。

long totalNanos = Long.parse( "1558439504711000000" ) ;

使用TimeUnit枚举执行拆分整秒的数学运算。

long secondsPortion = TimeUnit.NANOSECONDS.toSeconds( totalNanos ) ;

模数为十亿,其余为小数秒的纳秒。

long nanosPortion = ( totalNanos % 1_000_000_000L ) ;

实例化一个Instant

Instant instant = Instant.ofEpochSecond( secondsPortion , nanosPortion ) ;

我的时间戳以6个零结尾,这表明时间以纳秒为单位。

实际上,纳秒数高达十亿,因此九(9)位数字而不是六(6)位。从 epoch 开始计数的小数秒是 711000000,即 711,000,000 纳。您的整数秒数为 1558439504,即 1,558,439,504(15 亿)。作为小数:

自1970年1月1日01T00:00Z以来的1558439504.711000000秒

我遇到过一些例子,人们使用了我不需要的时区。

要表示时间轴上的某个时刻(特定点),您始终需要一个时区(或小时-分钟-秒的UTC偏移量)。

要通过特定地区(时区)的人们使用的挂钟时间查看同一时刻,请应用ZoneId以获得ZoneDateTime

大陆/地区格式指定适当的时区名称,例如美国/蒙特利尔非洲/卡萨布兰卡太平洋/奥克兰。切勿使用2-4个字母的缩写,例如BSTESTIST,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "Europe/London" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;  // Same moment, same point on the timeline, different wall-clock time.

2019-05-21T12:51:44.711 01:00[欧洲/伦敦]

请注意时间的调整,从小时 11 到小时 12。这是有道理的,因为欧洲/伦敦区域比该日期的 UTC 早一个小时。相同的时刻,时间轴上的相同点,不同的挂钟时间。

正如Ole V.V.在评论中指出的那样,您可以跳过上面讨论的数学。将整个纳秒数作为第二个参数提供给 ofEpochSecond。该类在内部进行数学运算,将整个秒与小数秒分开。

Instant instant = Instant.ofEpochSecond( 0L , 1_558_439_504_711_000_000L ) ;

请参阅此代码在 IdeOne.com 实时运行。

生成表示标准ISO 8601格式的ZonedDateTime的值的文本,扩展为在方括号中附加时区名称。

String output = zdt.toString() ;

2019-05-21T12:51:44.711 01:00[欧洲/伦敦]

或者让 java.time 自动为您本地化。

Locale locale = Locale.UK;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ).withLocale( locale );
String output = zdt.format( f );

21/05/2019, 12:51

或者指定自定义格式。

Locale locale = Locale.UK;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu HH:mm:ss" , locale ) ;
String output = zdt.format( f );

21-05-2019 12:51:44

提示:在不明确指定区域的情况下,提供日期时间时要非常小心。这会产生模糊性,用户可能会假设存在不同的区域/偏移。

 类似资料:
  • 我正在尝试将19位Unix时间戳(如(15亿分之一)转换为可读的日期/时间格式。我的时间戳以6个零结束,这表明时间以纳秒为单位。 我遇到过一些例子,人们使用了我不需要的时区。另一个例子是这样使用ofEpochSecond: 但是我不确定我是否需要使用ofEpoch秒钟。 下面的代码给出了我实现这一目标的最新方法: 但是我得到的输出是这样的: 它没有以例如2019格式显示年份格式。

  • 问题内容: 我有一个表示Python中的unix时间戳(即)的字符串,我想将其转换为可读的日期。使用时,我得到: 问题答案: 使用模块:

  • 问题内容: 如何在Java中将分钟从Unix时间戳转换为日期和时间。例如,时间戳1372339860对应于。 我想转换成。 编辑:其实我希望它是根据美国时间GMT-4,所以它将是。 问题答案: 你可以使用SimlpeDateFormat来格式化日期,如下所示: 如果使用的模式SimpleDateFormat非常灵活,则可以根据给定的特定模式,在javadocs中检入可用于产生不同格式的所有变体Da

  • 问题内容: 是否存在可用于将Unix时间戳转换为人类可读日期的MySQL函数?我有一个字段可以保存Unix时间,现在我想为人类可读的日期添加另一个字段。 问题答案: 用途: 另请参阅:的 MySQL文档。

  • 问题内容: 是否有32位系统的64位Unix时间戳转换的C ++实现?我需要转换为64位整数,反之亦然,包括leap年,时区和UTC。还需要它可移植,至少对于GNU / Linux和Windows而言。 问题答案: 你需要: 最初(2011年),此答案包含指向2038bug.com的链接,可以在其中下载包含上述功能的小型库。那时,该库已从2038bug.com中删除,链接断开并由主持人从答案中删除

  • 如何在Java中将分钟数从Unix时间戳转换为日期和时间?例如,时间戳对应于 。 我想将< code>1372339860转换为< code > 2013-06-27 13:31:00 GMT 。 编辑:实际上我希望它根据美国时间GMT-4,所以它将是。