如何将ISO8601格式转换为毫秒? [英] How to convert ISO8601 format into milliseconds?

查看:610
本文介绍了如何将ISO8601格式转换为毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶我还没有找到一种非常简单的方法来考虑在JSON中使用ISO8601的频率。

I'm pretty surprised that I haven't yet found a really easy way considering how often ISO8601 is used in JSON.

基本上,我正在服用看起来像这样的字符串: 2014-10-23T00:35:14.800Z 并将其转换为类似 50分钟之前

Basically, I'm taking a string that looks like this: 2014-10-23T00:35:14.800Z and converting it into something like 50 minutes ago.

首先,我必须将 2014-10-23T00:35:14.800Z 更改为 2014-10-23'T'00:35:14.800Z ,然后我需要将其转换为毫秒,然后很容易。

First, I have to change 2014-10-23T00:35:14.800Zto 2014-10-23'T'00:35:14.800Z, then I need to convert it to milliseconds, then it is easy.

我当前的代码:

private void setTimestamp(String timeCreated) {
    int indexOfT = timeCreated.indexOf('T');

    String properFormat = new StringBuilder(timeCreated).insert(indexOfT + 1, "'")
                                                        .insert(indexOfT, "'")
                                                        .toString();

    timeStamp = (String) DateUtils.getRelativeTimeSpanString(Long.parseLong(properFormat),
                                  System.currentTimeMillis(), 
                                  DateUtils.SECONDS_IN_MILLIS);
}

罪魁祸首是 Long.parseLong(properFormat)。我需要将 properFormat 转换为毫秒。

The culprit is Long.parseLong(properFormat). I need to convert properFormat into milliseconds.

推荐答案

tl; dr



tl;dr

Instant.parse( "2014-10-23T00:35:14.800Z" )
       .toEpochMilli()



java.time中的单行程



java.time 框架内置于Java 8及更高版本。这些新类取代了与最早版本的Java捆绑在一起的旧日期时间类,例如java.util.Date/.Calendar。请参阅教程。 java.time类还取代了非常成功的 Joda-Time 库,由一些人构建同样的人包括由同一个 Stephen Colbourne 领导。

One-Liner In java.time

The java.time framework is built into Java 8 and later. These new classes supplant the old date-time classes bundled with the earliest versions of Java such as java.util.Date/.Calendar. See Tutorial. The java.time classes also supplant the highly successful Joda-Time library, being built by some of the same folks including being led by the same Stephen Colbourne.

Instant UTC 中时间轴上的一个时刻,其分辨率为纳秒。您可以向它询问毫秒数来自纪元(1970年的第一个时刻, UTC )。但请记住, Instant 可能有额外的数据,纳秒比毫秒更精细。所以你可能会在一小部分时间内丢失数据

An Instant is a moment on the timeline in UTC with a resolution of nanoseconds. You can ask it for a count of milliseconds from its epoch (first moment of 1970 in UTC). But remember that an Instant may have additional data, nanoseconds being finer than milliseconds. So you may be losing data in that tiny fraction of a fraction of a second.

java.time类在使用标准ISO 8601格式时解析/生成字符串。无需指定格式模式。 Instant 类可以直接解析一个字符串。

The java.time classes use standard ISO 8601 formats when parsing/generating strings. No need to specify a formatting pattern. The Instant class can directly parse a string.

Instant.parse( "2014-10-23T00:35:14.800Z" )

您可以将其转换为自UTC 1970年第一时刻以来的毫秒,通过调用 toEpochMilli

You can convert that to a count of milliseconds since the epoch of first moment of 1970 in UTC by calling toEpochMilli

请注意可能的数据丢失即时类可以保持纳秒。因此,提取毫秒将截断任何小数秒内的任何微秒或纳秒。您的示例字符串在小数秒内只有三位数,因此只有几毫秒。但是当转换为毫秒计数时,小数部分的六位或九位将被截断为三位。

Be aware of possible data loss as the Instant class can hold nanoseconds. So extracting milliseconds will be truncating any microseconds or nanoseconds in any fractional second. Your example string has only three digits in the fractional second, so that is only milliseconds. But six or nine digits of decimal fraction would be truncated to three when converted to a count of milliseconds.

long millisFromEpoch = Instant.parse( "2014-10-23T00:35:14.800Z" ).toEpochMilli();

要按小时 - 分钟 - 秒计算已用时间,请使用 持续时间 class 。喂它的 之间的 方法一对时刻。

To get elapsed time in terms of hours-minutes-seconds, use the Duration class. Feed its between method a pair of moments in time.

Duration duration = Duration.between( Instant.parse( "2014-10-23T00:35:14.800Z" ) , Instant.now() );



Joda-Time中的单线



更新: Joda-Time项目处于维护模式,团队建议迁移到java.time类。

One-Liner In Joda-Time

UPDATE: The Joda-Time project is in maintenance mode, with the team advising migration to the java.time classes.

使用 Joda-Time 2.5库:

long millisSinceEpoch = new DateTime( "2014-10-23T00:35:14.800Z" ).getMillis();

Joda-Time解析并生成 ISO 8601 字符串。 Joda-Time适用于Android。众所周知,java.util.Date / .Calendar类很麻烦,容易混淆和存在缺陷。避免它们。

Joda-Time parses and generates ISO 8601 strings by default. Joda-Time works in Android. The java.util.Date/.Calendar classes are notoriously troublesome, confusing, and flawed. Avoid them.

这篇关于如何将ISO8601格式转换为毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆