Java日期以毫秒为单位 [英] Java Date to milliseconds

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

问题描述

我正在存储来自亚马逊云的消息,并按照时间戳在排序后的地图中对其进行排序.

I'm storing messages from an amazon cloud and ordering them by their timestamp in a sorted map.

我正在使用以下代码从云中解析时间戳:

I am parsing the timestamp from the cloud with the following code:

Date timestamp = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS'Z'", Locale.ENGLISH).parse(time);

然后我将它们存储在一个分类的地图中,其中的关键是日期.问题在于日期只能精确到秒.我可以在一秒钟内发送几条消息,因此我需要以毫秒为单位对它们进行排序.是否存在允许这种情况的数据结构?

and then I am storing in them in a sorted map with the key being the date. The issue is that the date only comes down to seconds precision. I can have several messages sent in 1 second, so I need them to be ordered with millisecond precision. Is there a data structure that allows this?

推荐答案

只要您的信号源具有高于1秒的分辨率即可.从模式中看起来像那样,但是您没有向我们展示任何输入示例.

Well as long as your source has a higher resolution than 1 second. Looks like that from the pattern, but you haven't shown us any input example.

Date 只是对自1970-01-01以来的 long 毫秒的包装.所以您已经拥有了. Date.getTime()将以毫秒精度返回该值.

Date is just a wrapper around a long milliseconds since 1970-01-01. So you have that already. Date.getTime() will return that, with millisecond precision.

您为什么会认为Date仅具有一秒的精度? Date.compareTo(Date anotherDate)进行毫秒级比较.因此,除非您做一些奇怪的事情,否则您的SortedMap应该可以正常工作.

Why would you think that Date only has one second precision? Date.compareTo(Date anotherDate) compares on a millisecond level. So your SortedMap should work fine unless you are doing something strange.

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

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