如何在java中将时间戳转换为日期和时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30994522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:28:58  来源:igfitidea点击:

How to convert timestamp into date and time in java?

javadatedatetimetimestamp

提问by pavaniiitn

I have a timestamp in json that is from a Linux server. I would like to convert it into a simple date-time format using Java.

我在 json 中有一个来自 Linux 服务器的时间戳。我想使用 Java 将其转换为简单的日期时间格式。

I need the date and time in the following format: dd-mm-yyyy hh:mm:ss

我需要以下格式的日期和时间:dd-mm-yyyy hh:mm:ss

here is my JSON data:

这是我的 JSON 数据:

[
  { 
    "batch_date": 1419038000
  }, 
  {
    "batch_date": 1419037000
  }
]

采纳答案by Scary Wombat

The batch date

批日期

"batch_date": 1419038000, 

looks like seconds from epoch,

看起来像纪元的几秒钟,

so

所以

new Date (batch_date * 1000); 

then use SimpleDateFormatshould do the trick

然后使用SimpleDateFormat应该可以解决问题

SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

-- code --

- 代码 -

    long batch_date = 1419038000; 
    Date dt = new Date (batch_date * 1000); 

    SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    System.out.println(sfd.format(dt));

-- output --

- 输出 -

20-12-2014 10:13:20

回答by kswaughs

when we want read/parse a string value as date, we should know the date format of that input string, otherwise we will get wrong date value.

当我们想要读取/解析一个字符串值作为日期时,我们应该知道该输入字符串的日期格式,否则我们会得到错误的日期值。

batch_date:1419038000 what is the date format of this value ?

batch_date:1419038000 这个值的日期格式是什么?

If we want to get the date value in the required format as in your case 'dd-mm-yyyy hh:mm:ss'. there are two possible ways.

如果我们想以所需格式获取日期值,如您的情况“dd-mm-yyyy hh:mm:ss”。有两种可能的方法。

1) request batch_date value should be in your required format. or

1) 请求 batch_date 值应采用您所需的格式。或者

2) if batch_date value is another format, we should know the original format, and build a Date object with original format using SimpleDateFormat parse method and then convert it into required format using another SimpleDateFormat format method.

2)如果batch_date值是另一种格式,我们应该知道原始格式,并使用SimpleDateFormat解析方法构建一个原始格式的Date对象,然后使用另一种SimpleDateFormat格式方法将其转换为所需格式。

回答by Basil Bourque

No Such Data Type

没有这样的数据类型

There is no such thing as "JSON timestamp". JSON has very few defined data types. No date-time types among them.

没有“JSON 时间戳”这样的东西。JSON定义的数据类型很少。其中没有日期时间类型。

As the correct Answerby Scary Wombat states, your number is apparently a count of whole seconds from the Unix epoch of 1970. The java.util.Date class tracks time as milliseconds since the Unix epoch, rather than whole seconds. So you need to multiply by 1,000. You also need to deal with longintegers (64-bit) rather than int(32-bit). Append an uppercase Lto the numeric literals, and declare any variables as long.

正如Scary Wombat的正确答案所述,您的数字显然是 1970 年 Unix 纪元的整秒计数。 java.util.Date 类将时间跟踪为自Unix 纪元以来的毫秒数,而不是整秒。所以你需要乘以1000。您还需要处理long整数(64 位)而不是int(32 位)。将大写附加L到数字文字,并将任何变量声明为long.

The java.util.Date/.Calendar classes are notoriously troublesome. They are now supplanted by the java.time packagebuilt into Java 8 and later, and/or the 3rd-party Joda-Timelibrary.

java.util.Date/.Calendar 类是出了名的麻烦。它们现在被Java 8 及更高版本中内置的java.time 包和/或第 3 方Joda-Time库所取代。

Various JSON-processing libraries support converters for creating java.time or Joda-Time objects. Or you can perform a conversion in your code, shown below.

各种 JSON 处理库支持用于创建 java.time 或 Joda-Time 对象的转换器。或者您可以在代码中执行转换,如下所示。

Be aware that both java.time and Joda-Time supports assigning a time zone. Code below assigns UTC for demonstration purposes, but you can assign your desired/expected zone.

请注意 java.time 和 Joda-Time 都支持分配时区。下面的代码为演示目的分配 UTC,但您可以分配您想要/预期的区域。

Joda-Time

乔达时间

Here is some code in Joda-Time2.8.1 showing the use of your input number as either seconds or milliseconds.

以下是Joda-Time2.8.1 中的一些代码,显示使用您的输入数字作为秒或毫秒。

long secondsSinceEpoch = 1419038000L;
DateTime dateTimeSeconds = new DateTime( secondsSinceEpoch , DateTimeZone.UTC );
DateTime dateTimeMillis = new DateTime( secondsSinceEpoch * 1000L , DateTimeZone.UTC );  // Note the crucial "L" appended to the numeric literal.

Dump to console.

转储到控制台。

System.out.println( "dateTimeSeconds: " + dateTimeSeconds );
System.out.println( "dateTimeMillis: " + dateTimeMillis );

When run.

跑的时候。

dateTimeSeconds: 1970-01-17T10:10:38.000Z
dateTimeMillis: 2014-12-20T01:13:20.000Z

java.time

时间

Similar code to above, but using java.time of Java 8.

与上面类似的代码,但使用 Java 8 的 java.time。

Instant instant = Instant.ofEpochSecond( 1419038000L );
ZonedDateTime zdtUtc = ZonedDateTime.ofInstant( instant , ZoneOffset.UTC );
ZonedDateTime zdtMontréal = zdtUtc.withZoneSameInstant( ZoneId.of( "America/Montreal" ) );

Dump to console.

转储到控制台。

System.out.println( "zdtUtc: " + zdtUtc );
System.out.println( "zdtMontréal: " + zdtMontréal );

When run.

跑的时候。

zdtUtc: 2014-12-20T01:13:20Z
zdtMontréal: 2014-12-19T20:13:20-05:00[America/Montreal]