在Java中将纪元秒转换为日期和时间格式

发布于 2024-12-18 10:03:46 字数 160 浏览 4 评论 0 原文

我有自 1970 年 1 月 1 日 UTC(大纪元时间)以来的秒数。

1320105600

我需要将秒数转换为以下格式的日期和时间。

Friday,November 4,2011 5:00,AM

我怎样才能实现这个目标?

I have seconds since 1970 january 1 UTC (Epoch time).

1320105600

I need to convert that seconds into date and time in below format.

Friday,November 4,2011 5:00,AM

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜吻♂芭芘 2024-12-25 10:03:46

如果您仅限于旧版 java.util.Datejava.util.Calendar API,则需要考虑到时间戳是以毫秒为单位解释的,而不是秒。因此,您首先需要将其乘以 1000 以获得以毫秒为单位的时间戳。

long seconds = 1320105600;
long millis = seconds * 1000;

通过这种方式,您可以将其提供给 java.util 的构造函数。日期并最终使用SimpleDateFormat 转换a将 java.util.Date 转换为 java.lang.String 以所需的日期格式模式,如有必要,使用预定义时区(否则将使用系统 默认时区,本身不是 GMT/UTC,因此格式化的时间可能已关闭)。

Date date = new Date(millis);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM

如果您已经使用 Java8,可以使用 LocalDateTime#ofEpochSecond() 它允许您直接提供纪元秒,而不需要乘以毫秒风格。

LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
String formattedDate = dateTime.format(formatter);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM

In case you're restricted to legacy java.util.Date and java.util.Calendar APIs, you need to take into account that the timestamps are interpreted in milliseconds, not seconds. So you first need to multiply it by 1000 to get the timestamp in milliseconds.

long seconds = 1320105600;
long millis = seconds * 1000;

This way you can feed it to a.o. the constructor of java.util.Date and finally use SimpleDateFormat to convert a java.util.Date to java.lang.String in the desired date format pattern, if necessary with a predefined time zone (otherwise it would use the system default time zone, which is not GMT/UTC per se and thus the formatted time might be off).

Date date = new Date(millis);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM

In case you're already on Java8, there's a LocalDateTime#ofEpochSecond() which allows you to feed epoch seconds directly without the need for multiplying into milliseconds flavor.

LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE,MMMM d,yyyy h:mm,a", Locale.ENGLISH);
String formattedDate = dateTime.format(formatter);
System.out.println(formattedDate); // Tuesday,November 1,2011 12:00,AM
ぇ气 2024-12-25 10:03:46
long yourSeconds = 1320105600L;
Date date = new Date(yourSeconds * 1000);

请参阅此 javadoc了解更多信息。构造函数需要毫秒。

要以适当的格式显示此日期,您应该检查 DateFormat< /a>

这是一个例子:

DateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
System.out.println(df.format(date));
long yourSeconds = 1320105600L;
Date date = new Date(yourSeconds * 1000);

See this javadoc for more info. The constructor needs milliseconds.

To display this date in an appropriate format you should check DateFormat

Here is an example:

DateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
System.out.println(df.format(date));
穿透光 2024-12-25 10:03:46

java.time

BalusC的回答很好,因为它指出您使用java.time。但该答案使用 LocalDateTime< /a> 其中 即时< /a> 更合适。 LocalDateTime不是时间线上的一个时刻,因为它故意没有 offset-from- 的概念UTC时区

java.time

java.time框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date、.Calendar 和 & 。 java.text.SimpleDateFormatJoda-Time 团队还建议迁移到 java.time。

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

许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7.0 并在 ThreeTenABP

即时

UTC 时间线上的某个时刻,分辨率高达 < a href="https://en.wikipedia.org/wiki/Nanosecond" rel="noreferrer">纳秒 由 Instant 类。

Instant instant = Instant.ofEpochSecond ( 1_320_105_600L );

转储到控制台。您的输入值是 2011 年 11 月 1 日 UTC 的第一时刻。末尾的 Z 是“Zulu”的缩写,表示 UTC。

System.out.println ( "instant: " + instant );

即时:2011-11-01T00:00:00Z

ZonedDateTime

在您的评论中,您提到希望通过 美国/芝加哥时区。使用正确的时区名称。应用时区 ZoneId 来获取 ZonedDateTime 对象。我们看到芝加哥在该日期比 UTC 晚五个小时。

ZoneId zoneId = ZoneId.of ( "America/Chicago" );
ZonedDateTime zdt = instant.atZone ( zoneId );

zdt: 2011-10-31T19:00-05:00[美国/芝加哥]

字符串

上面看到的字符串是标准的 ISO 8601 格式。要生成其他格式的字符串,请使用 DateTimeFormatter 类。您可以指定自己的自定义模式。但通常最好让 java.time 自动本地化为 Locale 对象。

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL );
formatter = formatter.withLocale ( Locale.US );
String output = zdt.format ( formatter );

2011 年 10 月 31 日星期一下午 7:00:00 美国中部时间

要指定您自己的自定义格式,请搜索 Stack Overflow 以获取许多示例和更多讨论。

java.time

The Answer by BalusC is good in that it points you to using java.time. But that Answer uses LocalDateTime where Instant is more appropriate. A LocalDateTime is not a moment on the timeline as it purposely has no concept of offset-from-UTC or time zone.

java.time

The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat. The Joda-Time team also advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

Instant

A moment on the timeline in UTC with a resolution up to nanoseconds is represented by the Instant class.

Instant instant = Instant.ofEpochSecond ( 1_320_105_600L );

Dump to console. Your input value is the first moment of November 1, 2011 in UTC. The Z on the end, short for 'Zulu', means UTC.

System.out.println ( "instant: " + instant );

instant: 2011-11-01T00:00:00Z

ZonedDateTime

In your comments you mention wanting to see this date-time through the lens of the America/Chicago time zone. Use a proper time zone name. Apply a time zone, ZoneId, to get a ZonedDateTime object. We see that Chicago is five hours behind UTC on that date.

ZoneId zoneId = ZoneId.of ( "America/Chicago" );
ZonedDateTime zdt = instant.atZone ( zoneId );

zdt: 2011-10-31T19:00-05:00[America/Chicago]

Strings

The Strings seen above are in standard ISO 8601 format. To generate strings in other formats, use the DateTimeFormatter class. You can specify your own custom pattern. But generally best to let java.time automatically localize to the human language and cultural norms encoded in a Locale object.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL );
formatter = formatter.withLocale ( Locale.US );
String output = zdt.format ( formatter );

Monday, October 31, 2011 7:00:00 PM CDT

To specify your own custom format, search Stack Overflow for many examples and more discussion.

青春如此纠结 2024-12-25 10:03:46
int seconds = 1320105600;
Date date = new Date(seconds * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a");
System.out.println(sdf.format(date));
int seconds = 1320105600;
Date date = new Date(seconds * 1000);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE,MMMM d,yyyy h:mm,a");
System.out.println(sdf.format(date));
七度光 2024-12-25 10:03:46

诀窍是使用 java.util.Date 和 java.text.DateFormat 来获取您想要的格式。您可以在网络教程中查找操作方法。

The trick is to use java.util.Date and java.text.DateFormat to get the format you want. You can look up how to do it in tutorials on the Web.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文