如何在 Java 中以毫秒为单位获取 ISO 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11933137/
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-10-31 07:00:03  来源:igfitidea点击:

How to get ISO format from time in milliseconds in Java?

javadate

提问by alicjasalamon

Is it simple way to get yyyy-MM-dd HH:mm:ss,SSSfrom time in millisecond? I've found some information how to do this from new Date()or Calendar.getInstance(), but couldn't find if it can be done from long (e.g. 1344855183166)

yyyy-MM-dd HH:mm:ss,SSS以毫秒为单位获取时间的简单方法是什么?我从new Date()or找到了一些如何做到这一点的信息Calendar.getInstance(),但找不到它是否可以从 long 开始(例如1344855183166

回答by theINtoy

I thought you had asked how to get the time in this format "yyyy-MM-dd HH:mm:ss,SSS"

我以为你问过如何以这种格式获取时间“yyyy-MM-dd HH:mm:ss,SSS”

One way is to use java's SimpleDateFormat: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

一种方法是使用 java 的 SimpleDateFormat:http: //docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

NOTE that this is not thread-safe.

请注意,这不是线程安全的。

...

...

Date d = new Date(1344855183166L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
String dateStr = sdf.format(d);

...

...

回答by Jigar Joshi

Use new Date(millis);constructor of Date

使用new Date(millis);构造函数Date

new Date(1344855183166L);

回答by Y__

The Dateconstructor does take a long (milliseconds) doesn't it?

Date构造函数需要很长的时间(毫秒),不是吗?

Regards,

问候,

回答by Basil Bourque

The question does not mention time zone, so I'll assume you meant UTC/GMT. The question does not explain "ISO format", so I'll assume you meant ISO 8601. This happens to be the default for third-party Joda-Time2.3 library. This isthread-safe.

这个问题没有提到时区,所以我假设你的意思是UTC/GMT。这个问题没有解释“ISO格式”,所以我假设你的意思是ISO 8601。这恰好是第三方Joda-Time2.3 库的默认设置。这线程安全的。

// ? 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

System.out.println( "That moment: " + new org.joda.time.DateTime( 1344855183166L, org.joda.time.DateTimeZone.UTC ) );

When run…

运行时…

That moment: 2012-08-13T10:53:03.166Z


If the original poster meant a Poland time zone…

如果原始海报的意思是波兰时区......

// ? 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Time Zone list… http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone warsawTimeZone = org.joda.time.DateTimeZone.forID( "Europe/Warsaw" );
System.out.println( "That moment in Poland: " + new org.joda.time.DateTime( 1344855183166L, warsawTimeZone ) );

When run…

运行时…

That moment in Poland: 2012-08-13T12:53:03.166+02:00

回答by Sridhar Sg

If you have epoch millis. The below line should work,

如果你有纪元毫秒。下面的行应该工作,

Instant.ofEpochMilli(millis).toString()