Java-SimpleDateFormat格式化程序以毫秒为单位返回时期 [英] Java - SimpleDateFormat formatter to return epoch time with milliseconds

查看:93
本文介绍了Java-SimpleDateFormat格式化程序以毫秒为单位返回时期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java和编码通常还是很陌生-我有一些代码以以下格式返回时间戳 yyyy.MM.dd HH:mm:ss:ms ,如下所示:

I am very new to Java and coding in general - I have some code which returns a timestamp in the following format yyyy.MM.dd HH:mm:ss:ms which is shown below:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss:sss");

返回值:

2017.07.19 11:42:30:423

有没有办法编辑上面的 SimpleDateFormat格式化程序代码以将日期/时间作为包含毫秒的纪元时间戳返回,以便按以下格式格式化返回的值?

Is there a way to edit the "SimpleDateFormat formatter" code above to return the date/time as an epoch timestamp that includes milliseconds so that the value returned is formatted as per the below?

1500464550423

我希望我可以修改( yyyy.MM.dd HH:mm:ss:sss)代码的 SimpleDateFormat格式化程序的一部分。

I'm hoping that I can amend the ("yyyy.MM.dd HH:mm:ss:sss") part of the SimpleDateFormat formatter code to do this.

任何帮助或建议都非常感谢。

Any help or advice is much appreciated.

谢谢

推荐答案

在格式模式字符串中使用大小写有一个简单错误(这些字符区分大小写)。更糟糕的是,您使用的是麻烦的 SimpleDateFormat 类。

You have a simple error in the use of case in your format pattern string (these are case sensitive). And worse, you are using the old and troublesome SimpleDateFormat class. One of the many problems with it is it’s not telling you what the problem is.

因此,我建议您使用现代的Java日期和时间API代替(我故意使用您的格式模式字串逐字显示):

So I recommend you use the modern Java date and time API instead (I am deliberately using your format pattern string verbatim):

    String receivedTimetamp = "2017.07.19 11:42:30:423";
    DateTimeFormatter parseFormatter
            = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss:sss");
    LocalDateTime dateTime = LocalDateTime.parse(receivedTimetamp, parseFormatter);
    System.out.println(dateTime);

此代码抛出 IllegalArgumentException:太多模式字母:s 。我希望这使您意识到以下事实:您正在使用2秒的秒,而使用3秒的秒。如果仍然不清楚,请该文档会告诉您小写的 s 在几秒钟内是正确的,而您需要大写的 S 作为分数。修复:

This code throws an IllegalArgumentException: Too many pattern letters: s. I hope this calls your awareness to the fact that you are using two s’s for seconds and three s’s for fraction of second. If it still isn’t clear, the documentation will tell you that lowercase s is correct for seconds, while you need uppercase S for the fraction. Let’s repair:

    DateTimeFormatter parseFormatter
            = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss:SSS");

现在代码打印 2017-07-19T11:42:30.423 ,因此我们设法正确地解析了字符串。

Now the code prints 2017-07-19T11:42:30.423, so we have managed to parse the string correctly.

要转换为毫秒,我们仍然缺少一条至关重要的信息:应该在哪个时区时间戳可以解释吗?我认为两个明显的猜测是UTC和您所在的时区(我不知道)。尝试UTC:

To convert to milliseconds we are still missing a crucial piece of information: in what time zone should the timestamp be interpreted? I think the two obvious guesses are UTC and your local time zone (which I don’t know). Try UTC:

    System.out.println(dateTime.atOffset(ZoneOffset.UTC).toInstant().toEpochMilli());

这将生成 1500464550423 你要的。我想我们已经完成了。

This produces 1500464550423, which is the number you asked for. I suppose we’re done.

如果您想要设置JVM的时区,请使用 .atZone(ZoneId.systemDefault())而不是 .atOffset(ZoneOffset.UTC),但是请注意,该设置可能会被在同一JVM中运行的其他软件更改,因此这很脆弱。

If you wanted your JVM’s time zone setting instead, use .atZone(ZoneId.systemDefault()) instead of .atOffset(ZoneOffset.UTC), but beware that the setting may be altered by other software running in the same JVM, so this is fragile.

这篇关于Java-SimpleDateFormat格式化程序以毫秒为单位返回时期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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