将字符串时间戳转换为符合ISO 8601的字符串 [英] convert string timestamp to ISO 8601 compliant string

查看:293
本文介绍了将字符串时间戳转换为符合ISO 8601的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间戳和字符串格式的偏移量,如下所示,在两个不同的变量中显示:

01/14/2016 07:37:36PM
-08:00

我想将上述时间戳转换为ISO 8601 compliant String, with milliseconds and timezone,因此转换后应如下所示:

2016-01-14T19:37:36-08:00

我该怎么做?我正在使用jodatime库.

解决方案

较新的java.time类与ISO 8601字符串配合得很好.

    String dateTimeString = "01/14/2016 07:37:36PM"; 
    String offsetString = "-08:00";

    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString,
            DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ssa"));
    ZoneOffset offset = ZoneOffset.of(offsetString);
    String formattedTimestamp = dateTime.atOffset(offset).toString();
    System.out.println(formattedTimestamp);

此打印

2016-01-14T19:37:36-08:00

远离过时的课程,例如SimpleDateFormat.

什么是offsetString不存在?我了解在这种情况下,您希望UTC的偏移量为Z.例如这样的

    ZoneOffset offset;
    if (offsetString == null) {
        offset = ZoneOffset.UTC;
    } else {
        offset = ZoneOffset.of(offsetString);
    }
    String formattedTimestamp = dateTime.atOffset(offset).toString();

有了null offsetString,我们现在得到了

2016-01-14T19:37:36Z

java.time中的类(我正在使用其中的一些)在JSR-310中进行了描述,并且是Java 8内置的.如果您想在Java 6或7中使用它们,该怎么办?您将获得ThreeTen Backport(下面的链接).它为您提供了Java 6和7的大多数类.我不太高兴告诉您您需要一个外部库,但是在这种情况下,只有到您使用Java 8为止,我希望您会很快. >

我确定也可以使用JodaTime完成此操作,但是我没有相关经验,因此无法在此提供详细信息.我所知道的,我已经阅读了JodaTime背后的人们,现在建议您改为使用java.time.因此,我想请您将一个外部库换成一个新的(据说是更好的)库.就其本身而言,我并不为此感到不满意.仅当您已经具有使用JodaTime的代码库时,它才是真正的琐事.

链接: ThreeTen Backport

I have a timestamp and offset in string format as shown below in two different variables:

01/14/2016 07:37:36PM
-08:00

I want to convert above timestamp into ISO 8601 compliant String, with milliseconds and timezone so it should look like this after conversion:

2016-01-14T19:37:36-08:00

How can I do that? I am using jodatime library.

解决方案

The newer java.time classes work so well with ISO 8601 strings.

    String dateTimeString = "01/14/2016 07:37:36PM"; 
    String offsetString = "-08:00";

    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString,
            DateTimeFormatter.ofPattern("MM/dd/uuuu hh:mm:ssa"));
    ZoneOffset offset = ZoneOffset.of(offsetString);
    String formattedTimestamp = dateTime.atOffset(offset).toString();
    System.out.println(formattedTimestamp);

This prints

2016-01-14T19:37:36-08:00

Stay away from outdated classes like SimpleDateFormat.

What is offsetString is not present? I understand that in this case you want an offset of Z for UTC. For example like this:

    ZoneOffset offset;
    if (offsetString == null) {
        offset = ZoneOffset.UTC;
    } else {
        offset = ZoneOffset.of(offsetString);
    }
    String formattedTimestamp = dateTime.atOffset(offset).toString();

With a null offsetString we now get

2016-01-14T19:37:36Z

The classes in java.time (of which I’m using but a few) are described in JSR-310 and come built-in with Java 8. What if you would like to use them with Java 6 or 7? You get the ThreeTen Backport (link below). It gives you the majority of the classes for Java 6 and 7. I’m not perfectly happy to tell you you need an external library, but in this case it’s only until you move to Java 8. I hope you will soon.

I am sure it can be done with JodaTime too, but I haven’t got experience with it, so cannot give you the details there. What I do know, I have read the the folks behind JodaTime now recommend you move over to java.time instead. So I am asking you to swap one external library for a newer (and supposedly better) one. In itself I’m not unhappy with that. Only if you already have a codebase that uses JodaTime, it’s not really trivial.

Link: ThreeTen Backport

这篇关于将字符串时间戳转换为符合ISO 8601的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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