简体   繁体   English

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

[英]How to get ISO format from time in milliseconds in Java?

Is it simple way to get yyyy-MM-dd HH:mm:ss,SSS from time in millisecond?以毫秒为单位获取yyyy-MM-dd HH:mm:ss,SSS简单方法吗? 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 (eg 1344855183166 )我从new Date()Calendar.getInstance()找到了一些如何做到这一点的信息,但找不到它是否可以从 long 完成(例如1344855183166

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);

... ...

If you have epoch millis.如果你有纪元毫秒。 The below line should work,下面的行应该工作,

Instant.ofEpochMilli(millis).toString()

As said by Sridhar Sg's the code:正如 Sridhar Sg 所说的代码:

Instant.ofEpochMilli(millis).toString()

will work as the toString() method will give you the ISO-8601 extended format representation (with separators).将工作,因为toString()方法将为您提供 ISO-8601 扩展格式表示(带分隔符)。

Note that the Instant class will only work from JDK 8 (introduction of the java.time package) unless you use ThreeTen Backport , the backport to Java 6 and 7.请注意,除非您使用ThreeTen Backport(向 Java 6 和 7 的反向移植),否则Instant只能从 JDK 8 (引入java.time包)开始工作。

If millis = 1603101879234 the above method will return: 2020-10-19T10:04:39.234Z如果millis = 1603101879234 上述方法将返回: 2020-10-19T10:04:39.234Z

If you need other kind of format, like the ISO-8601 basic format (without separators except the T in the middle) you can use a custom DateTimeFormatter like this:如果您需要其他类型的格式,例如 ISO-8601 基本格式(除了中间的 T 之外没有分隔符),您可以像这样使用自定义DateTimeFormatter

Instant instant = Instant.ofEpochMilli(millis);
DateTimeFormatter outFormatter = DateTimeFormatter
        .ofPattern("yyyyMMdd'T'HHmmss.SSSX") // millisecond precision
        .withZone(ZoneId.of("UTC"));

String basicIso = outFormatter.format(instant);

For the same millis = 1603101879234 the above method will yield: 20201019T100439.234Z .对于相同的毫秒 = 1603101879234,上述方法将产生: 20201019T100439.234Z

The Date constructor does take a long (milliseconds) doesn't it? Date构造函数确实需要很长时间(毫秒),不是吗?

Regards,问候,

The question does not mention time zone, so I'll assume you meant UTC /GMT.这个问题没有提到时区,所以我假设你的意思是UTC /GMT。 The question does not explain "ISO format", so I'll assume you meant ISO 8601 .这个问题没有解释“ISO格式”,所以我假设你的意思是ISO 8601 This happens to be the default for third-party Joda-Time 2.3 library.这恰好是第三方Joda-Time 2.3 库的默认设置。 This is thread-safe.线程安全的。

// © 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

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

new Date(1344855183166L);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM