将日期时间字符串转换为Unix时间戳 [英] convert date time string to unix timestamp

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

问题描述

我有一个日期时间字符串,如下所示,它们之间没有空格:

I have a string for date time coming as like this without any spaces in between them:

TueDec2618:47:09UTC2017

是否可以将其转换为unix时间戳?日期时间字符串将始终采用该格式.

Is there any way to convert this to unix timestamps? The date time string will always be in that format.

推荐答案

java.time和ThreeTen Backport

ernest_k的评论经过深思熟虑,解决了您的问题:

java.time and ThreeTen Backport

The comment by ernest_k is well thought out and solves your issue:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
    String dateTimeString = "TueDec2618:47:09UTC2017";
    ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeString, formatter);
    long unixTimestamp = dateTime.toEpochSecond();
    System.out.println("Parsed date-time " + dateTime + " Unix timestamp " + unixTimestamp);

在Java 1.7.0_79上测试的在ThreeTen Backport 1.3.6上运行的输出是:

The output from running on ThreeTen Backport 1.3.6, tested on Java 1.7.0_79, is:

解析日期时间2017-12-26T18:47:09Z [Zulu] Unix时间戳1514314029

Parsed date-time 2017-12-26T18:47:09Z[Zulu] Unix timestamp 1514314029

问题:如何在Java 7上使用ZonedDateTime和DateTimeFormatter?

我仍然在Java 7上运行,所以不能使用ZonedDataTimeDateTimeFormatter但我可以使用joda库.

I am still on Java 7 btw so can't use ZonedDataTime and DateTimeFormatter but I can use joda library.

的确可以. java.time只需要至少 Java 6 .

Indeed you can. java.time just requires at least Java 6.

  • 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
  • 在Java 6和7中,获得了ThreeTen反向端口,这是现代类的反向端口(JSR 310的ThreeTen;请参见底部的链接).
  • 在(较旧的)Android上,请使用Android版的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从org.threeten.bp导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

尽管Joda-Time是另一个不错的解决方案,但我认为您应该选择ThreeTen Backport而不是Joda-Time(尽管意见分歧). Joda-Time主页建议:

While Joda-Time would be another nice solution, I believe that you should prefer the ThreeTen Backport over Joda-Time (though opinions differ). The Joda-Time home page advises:

请注意,Joda-Time被认为是很大程度上完成"的项目. 没有计划进行重大增强.如果使用Java SE 8,请迁移 到java.time(JSR-310).

Note that Joda-Time is considered to be a largely "finished" project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

所以java.time似乎是面向未来的解决方案.

So java.time seems to be the future-proof solution.

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
  • Joda-Time home page

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

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