[英]Java utc milliseconds
Trying to get Universal Time in java seems to be so difficult. 试图在java中获得通用时间似乎非常困难。 Something like this in C#
在C#中有类似的东西
DateTime.Now.ToUniversalTime()
seems to be something so difficult. 似乎有点困难。 I have code that subtracts a current utc time from a earlier date that is also utc to find the difference in time.
我有一些代码从较早的日期中减去当前的utc时间,该日期也是为了找到时间上的差异。 But I can't seem to see how to get the current utc time.
但我似乎无法看到如何获得当前的时间。 this is my current code
这是我目前的代码
Date date = new Date();
long difference = date.getTime() - s.getTime();
s is already in utc time because it comes from a source that is passing me the utc time s已经在utc时间了,因为它来自一个传递给我时间的来源
Give this a try: 尝试一下:
Date utcDate = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
Best, Loddi 最好,Loddi
Current moment in UTC: UTC当前时刻:
Instant.now()
Elapsed time: 经过的时间:
Instant then = … ;
Instant now = Instant.now();
Duration duration = Duration.between( then , now ); // For days-hours-minutes-seconds scale.
Render as a count of milliseconds. 渲染为毫秒计数。
long millis = duration.toMillis(); // Possible data-loss in truncating nanoseconds to milliseconds.
The modern approach to date-time handling in Java is the java.time classes. Java中日期时间处理的现代方法是java.time类。
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant
类表示UTC时间轴上的一个时刻,分辨率为纳秒 (最多九(9)位小数)。
Instant instant = Instant.now(); // Current moment in UTC.
The Duration
class represents a span of time unattached to the timeline. Duration
类表示未附加到时间轴的时间跨度。 You can feed in a pair of Instant
objects to calculate the number of days, hours, minutes, seconds, and nanoseconds in between. 您可以输入一对
Instant
对象来计算两者之间的天数,小时数,分钟数,秒数和纳秒数。
Duration duration = Duration.between( thisInstant , thatInstant );
Call toString
to generate a string in standard ISO 8601 format of PnYnMnDTnHnMnS
where P
marks the beginning and T
separates any years-months-days from the hours-minutes-seconds. 调用
toString
以PnYnMnDTnHnMnS
标准ISO 8601格式生成字符串,其中P
标记开头, T
将小时 - 分钟 - 秒的任何年 - 月 - 天分开。 For example, an hour and a half is PT1H30M
. 例如,一个半小时是
PT1H30M
。
You can get the total number of milliseconds in this duration. 您可以获得此持续时间内的总毫秒数。 But beware of possible data-loss.
但要注意可能的数据丢失。 The
Instant
and Duration
classes work in a resolution of nanoseconds , much finer than milliseconds . Instant
和Duration
类的工作分辨率为纳秒 ,比毫秒更精细。 So extracting milliseconds will truncate any fraction of a second beyond the milliseconds. 因此,提取毫秒将截断超过毫秒的任何一秒。 In other words, rather than having a split second with a decimal representation of up to nine digits, you will have only up to three digits.
换句话说,您最多只能有三位数,而不是具有最多九位数的十进制表示。
long millis = duration.toMillis(); // Possible data-loss in truncating nanoseconds to milliseconds.
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧遗留日期时间类,如
java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to java.time. 现在处于维护模式的Joda-Time项目建议迁移到java.time。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 。 And search Stack Overflow for many examples and explanations.
并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 .
规范是JSR 310 。
Where to obtain the java.time classes? 从哪里获取java.time类?
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time.
该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as
Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如
Interval
, YearWeek
, YearQuarter
,和更多 。
UPDATE: The Joda-Time project, now in maintenance mode , advises migration to java.time. 更新:现在处于维护模式的Joda-Time项目建议迁移到java.time。 Leaving this section intact for history.
留下这部分完整的历史。
Using the Joda-Time 2.4 library. 使用Joda-Time 2.4库。
DateTime nowUtc = DateTime.now( DateTimeZone.UTC );
Best to avoid java.util.Date and .Calendar as they are notoriously troublesome. 最好避免使用java.util.Date和.Calendar,因为它们非常麻烦。 But if you must, you can convert.
但如果你必须,你可以转换。
Date date = nowUtc.toDate();
As for getting a difference between date-time values, search StackOverflow for hundreds of answers. 至于获取日期时间值之间的差异,请搜索数百个答案的StackOverflow。 Joda-Time offers 3 classes for representing a span of time: Interval, Period, and Duration.
Joda-Time提供3个类来表示时间跨度:间隔,周期和持续时间。
Date is in UTC. 日期是 UTC。 There's no time zone attached to Date.
没有时区附加到日期。
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime() http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
返回自此Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
What makes you think it's in local time? 是什么让你觉得它是在当地时间?
Edit: The documentation does say this, so maybe that's the problem: 编辑:文档说这个,所以也许这就是问题所在:
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine.
尽管Date类旨在反映协调世界时(UTC),但它可能不会完全这样做,具体取决于Java虚拟机的主机环境。
It looks like this might be a good solution: 看起来这可能是一个很好的解决方案:
Calendar c = Calendar.getInstance();
int utcOffset = c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET);
Long utcMilliseconds = c.getTimeInMillis() + utcOffset;
You can use follwing method to find UTC time: 您可以使用follwing方法查找UTC时间:
Calender c= Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Then you can get month,day ,time ,year ant from the calender object.. ex: c.get(Calender.YEAR)
然后你可以从日历对象中得到月,日,时间,年蚂蚁.. ex:
c.get(Calender.YEAR)
Hope this will help you.. 希望这个能对您有所帮助..
Hmm, no one mentioned System.currentTimeMillis()
so i will :) This will return the number of millis since the Unix epoch. 嗯,没有人提到
System.currentTimeMillis()
所以我会:)这将返回自Unix时代以来的毫秒数。 This will always be in UTC (eg if you call it at the same time in London and in Athens you will get the same values - if the times are set correctly on those devices the timezone shouldn't matter). 这将始终为UTC(例如,如果您在伦敦和雅典同时调用它,您将获得相同的值 - 如果在这些设备上正确设置时间,则时区无关紧要)。 If you want to obtain a Calendar instance from a number of millis you can just do something like:
如果您想从多个millis获取Calendar实例,您可以执行以下操作:
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(yourMillis);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.