Java 如何将 YYYYMMDDHHMMSS 格式的日期转换为纪元或 unix 时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23394024/
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
How can I convert a date in YYYYMMDDHHMMSS format to epoch or unix time?
提问by Cormac Q
EDIT: I have edited my question to include more information, I have tried many ways to do this already, asking a question on StackOverflow is usually my last resort. Any help is greatly appreciated.
编辑:我已经编辑了我的问题以包含更多信息,我已经尝试了很多方法来做到这一点,在 StackOverflow 上提问通常是我最后的手段。任何帮助是极大的赞赏。
I have a date (which is a Timestamp object) in a format of YYYYMMDDHHMMSS (e.g. 20140430193247). This is sent from my services to the front end which displays it in the format: date:'dd/MM/yyyy' using AngularJS.
我有一个格式为 YYYYMMDDHHMMSS(例如 20140430193247)的日期(这是一个 Timestamp 对象)。这是从我的服务发送到前端,前端以以下格式显示它:日期:'dd/MM/yyyy' 使用 AngularJS。
How can I convert this into Epoch/Unix time?
如何将其转换为纪元/Unix 时间?
I have tried the duplicate question that is linked to this, what I get returned is a different date.
我已经尝试了与此相关的重复问题,我得到的是一个不同的日期。
I have also tried the following:
我还尝试了以下方法:
A:
A:
//_time == 20140430193247
return _time.getTime()/1000; // returns 20140430193 - INCORRECT
B:
乙:
return _time.getTime(); // returns 20140430193247 (Frontend: 23/03/2608) - INCORRECT
C:
C:
Date date = new Date();
//_time = 20140501143245 (i.e. 05/01/2014 14:32:45)
String str = _time.toString();
System.out.println("Time string is " + str);
//Prints: Time string is 2608-03-24 15:39:03.245 meaning _time.toString() cannot be used
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
date = df.parse(str);
} catch (ParseException e) {
}
return date; // returns 20140501143245 - INCORRECT
D:
乙:
date = new java.sql.Date(_time.getTime());
return date; // returns 2608-03-24 - INCORRECT
The following shows the todays date correctly:
以下内容正确显示了今天的日期:
Date date = new Date();
return date; // returns 1398939384523 (Frontend: 01/05/2014)
Thanks
谢谢
采纳答案by Cormac Q
I got the answer after quite a while of trying different ways. The solution was pretty simple - to parse the time to a string as toString() didn't work.
经过一段时间尝试不同的方法后,我得到了答案。解决方案非常简单 - 将时间解析为字符串,因为 toString() 不起作用。
Date date;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
try {
date = df.parse(String.valueOf(_time.getTime()));
} catch (ParseException e) {
throw new RuntimeException("Failed to parse date: ", e);
}
return date.getTime();
回答by Basil Bourque
tl;dr
tl;博士
LocalDateTime
.parse(
"20140430193247" ,
DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" )
)
.atOffset(
ZoneOffset.UTC
)
.toEpochSecond()
java.time
时间
Parse your input string as a LocalDateTime
as it lacks an indicator of offset-from-UTC or time zone.
将您的输入字符串解析为 a,LocalDateTime
因为它缺少从 UTC 或时区偏移的指示符。
String input = "20140430193247" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
Now we have a date with time-of-day, around half-past 7 PM on April 30 of 2014. But we lack the context of offset/zone. So we do not know if this was 7 PM in Tokyo Japan or 7 PM in Toledo Ohio US, two different moments that happened several hours apart.
现在我们有了一个包含时间的日期,大约是 2014 年 4 月 30 日晚上 7 点半左右。但是我们缺少偏移/区域的上下文。所以我们不知道这是日本东京的晚上 7 点还是美国俄亥俄州托莱多的晚上 7 点,这两个不同的时刻相隔了几个小时。
To determine a moment, you must know the intended offset/zone.
要确定一个时刻,您必须知道预期的偏移/区域。
If you know for certain that an offset of zero, or UTC itself, was intended, apply the constant ZoneOffset.UTC
to get an OffsetDateTime
.
如果您确定要使用零偏移量或 UTC 本身,请应用常量ZoneOffset.UTC
以获取OffsetDateTime
.
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;
How can I convert this into Epoch/Unix time?
如何将其转换为纪元/Unix 时间?
Do you mean a count of seconds or milliseconds since the first moment of 1970 in UTC?
您的意思是自 UTC 时间 1970 年的第一时刻以来的秒数或毫秒数吗?
For a count of whole seconds since 1970-01-01T00:00Z, interrogate the OffsetDateTime
object.
对于自 1970-01-01T00:00Z 以来的整秒计数,询问OffsetDateTime
对象。
long secondsSinceEpoch = odt.toEpochSecond() ;
For milliseconds, extract a Instant
object. An Instant
represents a moment in UTC, and is the basic building-block class of java.time. Then interrogate for the count.
对于毫秒,提取一个Instant
对象。AnInstant
代表 UTC 中的一个时刻,是java.time的基本构建块类。然后询问计数。
Instant instant = odt.toInstant() ;
long millisSinceEpoch = instant.toEpochMilli() ;