日期转换为纪元Java [英] Conversion of a date to epoch Java

查看:111
本文介绍了日期转换为纪元Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 2018-02-21 15:47:35 UTC转换为时代UTC形式。我们该怎么做呢?我目前在PST中。

I want to convert 2018-02-21 15:47:35 UTC to epoch UTC form. How do we do it? I am currently in PST.

SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");

df.setTimeZone(TimeZone.getTimeZone("UTC"));
date = df.parse(dateString).getTime();

上面的代码应返回自自1970年1月1日以来的毫秒数, 00:00:00 GMT ,但我输入的值不正确。

The code above should return the number of milliseconds since January 1, 1970, 00:00:00 GMT, but I'm getting an incorrect value.

推荐答案

预期:2018年2月21日15:47:35 UTC等于自1970年1月1日0:00 UTC以来的1 519 228 055 000毫秒。

Expected: 2018-02-21 15:47:35 UTC is equivalent to 1 519 228 055 000 milliseconds since the epoch of January 1, 1970 at 0:00 UTC.

已观察:问题中的代码给出了1 514 818 800035。因此,它比4 409 254 965毫秒减少了51天。

Observed: Your code in the question gives 1 514 818 800 035. So it’s 4 409 254 965 milliseconds off, a little over 51 days.

解决方案

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    date = LocalDateTime.parse("2018-02-21 15:47:35", dtf)
            .atOffset(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();

这给出了正确的1 519 228 055 000。

This gives the correct 1 519 228 055 000.

出了什么问题?

SimpleDateFormat 是它的默认设置,如果您指定了不正确的格式模式字符串,它通常会给您不正确的结果,并假装一切都很好。我在代码段中使用的现代Java日期和时间API试图更加努力地找出什么时候该模式没有意义,并以某种方式告诉您错误。例如,让我们尝试使用现代的 DateTimeFormatter 设置格式:

One of the many troublesome traits of SimpleDateFormat is that with its default settings, if you specify an incorrect format pattern string, it will very often give you an incorrect result and pretend all is well. The modern Java date and time API that I am using in my snippet, is trying somewhat harder to figure out when the pattern doesn’t make sense and tell you it’s wrong somehow. As an example, let’s try your format pattern with the modern DateTimeFormatter:

    final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("YYYY-MM-DD HH:MM:SS");
    LocalDateTime.parse(dateString, dtf);

这将抛出 java.time.format.DateTimeParseException:Text'2018 -02-21 15:47:35'无法在索引14 进行解析。索引14是字符串中47的位置,应该是分钟。显然47个格式与 MM 不匹配。如果尚未确定,请查看文档。它说大写的 M 是一年中的月份。因此,格式化程序要告诉您的是一年没有47个月。在文档中,您还会发现小写的 m 表示分钟。当您更正格式模式字符串中字母的大小写时,您将收到其他异常,直到最后得到 yyyy-MM-dd HH:mm:ss uuuu-MM-dd HH:mm:ss (小写字母 yyyy 是年份或时代,而 uuuu 是签名年份,都在第0年以后的年份工作。)

This will throw a java.time.format.DateTimeParseException: Text '2018-02-21 15:47:35' could not be parsed at index 14. Index 14 is where 47 is in the string, it was supposed to be the minutes. Apparently 47 doesn’t match MM in the format. If you haven’t figured out yet, check the documentation. It says that uppercase M is for "month-of-year". So what the formatter is trying to tell you is there are not 47 months in a year. In the documentation you will also find lowercase m for "minute-of-hour". As you correct the case of the letters in the format pattern string, you will receive other exceptions until you end up with either yyyy-MM-dd HH:mm:ss or uuuu-MM-dd HH:mm:ss (lowercase yyyy is year or era while uuuu is a signed year, both work for years after year 0).

链接

  • Oracle tutorial: Date Time explaining how to use java.time.
  • DateTimeFormatter documentation spelling out the uppercase and lowercase letters of format pattern strings.

这篇关于日期转换为纪元Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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