将unix时间戳转换为java中的日期[复制]

时间:2021-08-06 17:02:19

This question already has an answer here:

这个问题在这里已有答案:

How can I convert minutes from unix time stamp to date and time in java. For example, time stamp 1372339860 correspond to Thu, 27 Jun 2013 13:31:00 GMT.

如何在Unix中将分钟从unix时间戳转换为日期和时间。例如,时间戳1372339860对应于格林威治标准时间2013年6月27日星期三13:31:00。

I want to convert 1372339860 to 2013-06-27 13:31:00 GMT.

我想将1372339860兑换成2013-06-27 13:31:00 GMT。

Edit : Actually I want it to be according to US timing GMT-4, so it will be 2013-06-27 09:31:00.

编辑:其实我希望它是根据美国时间GMT-4,所以它将是2013-06-27 09:31:00。

3 个解决方案

#1


123  

You can use SimlpeDateFormat to format your date like this:

您可以使用SimlpeDateFormat格式化您的日期,如下所示:

long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L); 
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4")); 
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

如果非常灵活,SimpleDateFormat采用的模式,您可以在javadocs中检查您可以使用的所有变体,根据您在给定特定日期时编写的模式生成不同的格式。 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

  • Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
  • 因为Date提供了一个getTime()方法,它返回自EPOC以来的毫秒数,所以您需要为SimpleDateFormat提供一个时区来根据您的时区正确设置日期格式,否则它将使用JVM的默认时区(如果有的话)无论如何都配置好吧)

#2


27  

Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:

Java 8引入了Instant.ofEpochSecond实用程序方法,用于从Unix时间戳创建Instant,然后可以将其转换为ZonedDateTime并最终格式化,例如:

final DateTimeFormatter formatter = 
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
        .atZone(ZoneId.of("GMT-4"))
        .format(formatter);

System.out.println(formattedDtm);   // => '2013-06-27 09:31:00'

I thought this might be useful for people who are using Java 8.

我认为这可能对使用Java 8的人有用。

#3


10  

You need to convert it to milliseconds by multiplying the timestamp by 1000:

您需要将时间戳乘以1000将其转换为毫秒:

java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);

#1


123  

You can use SimlpeDateFormat to format your date like this:

您可以使用SimlpeDateFormat格式化您的日期,如下所示:

long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L); 
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4")); 
String formattedDate = sdf.format(date);
System.out.println(formattedDate);

The pattern that SimpleDateFormat takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

如果非常灵活,SimpleDateFormat采用的模式,您可以在javadocs中检查您可以使用的所有变体,根据您在给定特定日期时编写的模式生成不同的格式。 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

  • Because a Date provides a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
  • 因为Date提供了一个getTime()方法,它返回自EPOC以来的毫秒数,所以您需要为SimpleDateFormat提供一个时区来根据您的时区正确设置日期格式,否则它将使用JVM的默认时区(如果有的话)无论如何都配置好吧)

#2


27  

Java 8 introduces the Instant.ofEpochSecond utility method for creating an Instant from a Unix timestamp, this can then be converted into a ZonedDateTime and finally formatted, e.g.:

Java 8引入了Instant.ofEpochSecond实用程序方法,用于从Unix时间戳创建Instant,然后可以将其转换为ZonedDateTime并最终格式化,例如:

final DateTimeFormatter formatter = 
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

final long unixTime = 1372339860;
final String formattedDtm = Instant.ofEpochSecond(unixTime)
        .atZone(ZoneId.of("GMT-4"))
        .format(formatter);

System.out.println(formattedDtm);   // => '2013-06-27 09:31:00'

I thought this might be useful for people who are using Java 8.

我认为这可能对使用Java 8的人有用。

#3


10  

You need to convert it to milliseconds by multiplying the timestamp by 1000:

您需要将时间戳乘以1000将其转换为毫秒:

java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);