Java utc毫秒 [英] Java utc milliseconds

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

问题描述

试图在Java中获得Universal Time似乎是如此困难。在C#中这样的东西

  DateTime.Now.ToUniversalTime()

似乎有些困难。我有一个代码,从较早的日期减去当前的utc时间,也是utc来找到时间差异。但我似乎看不到如何获得当前的utc时间。这是我目前的代码

  Date date = new Date(); 
long difference = date.getTime() - s.getTime();

s 已经在utc时间,因为它源自通过我的utc时间

解决方案

tl; dr



在UTC中:

  Instant.now()

已过期:

  Instant then = ...; 
立即即时= Instant.now();

持续时间=持续时间(之后) //对于几天 - 小时 - 分钟秒的比例。

呈现为毫秒数。

  long millis = duration.toMillis(); //将数据截断纳秒的数据丢失到毫秒。 



java.time



现代方法在Java中进行日期处理是java.time类。



即时



Instant 类代表 UTC时间线上的一段时间,其分辨率为纳秒()(最多九(9)位十进制分数)。

 即时即时= Instant.now(); // UTC当前时刻

持续时间类表示时间跨度未连接到时间轴。您可以使用一对 Instant 对象来计算其间的天数,小时,分钟,秒和纳秒。

 持续时间=持续时间(thisInstant,thatInstant); 

调用 toString 以生成标准的字符串 ISO 8601 格式的 PnYnMnDTnHnMnS 其中 P 标记开头,而 T 将从几分钟到秒之间的任何年 - 月 - 日分开。例如,一个半小时是 PT1H30M



您可以获得此持续时间内的总毫秒数。但请注意可能的数据丢失。 Instant 持续时间类工作在 nanoseconds ,比毫秒< a>。因此,提取毫秒将截断超过毫秒的秒数。换句话说,而不是使用十进制表示最多九位的分秒,您只能有三位数字。

  long millis = duration.toMillis(); //将数据截断纳秒的数据丢失到毫秒。 






关于java.time



java.time 框架内置于Java 8及更高版本中。这些课程取代了麻烦的旧旧版日期时间课程,例如 java.util.Date 日历 ,& SimpleDateFormat



Joda-Time 项目,现在在维护模式中,建议迁移到java.time 。



要了解更多信息,请参阅 Oracle教程。并搜索Stack Overflow的许多示例和解释。规格为 JSR 310



哪里可以获取java.time类?





ThreeTen-Extra 项目扩展了java.time和其他类。这个项目是未来可能添加到java.time的证明。您可能会在这里找到一些有用的课程,例如 Interval YearWeek YearQuarter more






Joda-Time < h1>

更新: Joda-Time 项目,现在在维护模式中,建议迁移到java.time。离开这一部分完整的历史。



使用 Joda-Time 2.4 library。

  DateTime nowUtc = DateTime.now(DateTimeZone.UTC); 

最好避免java.util.Date和.Calendar,因为它们是臭名昭着的麻烦。但是如果你必须,你可以转换。

  Date date = nowUtc.toDate(); 

为了获取日期时间值之间的差异,请搜索StackOverflow以获得数百个答案。 Joda-Time提供3个课程,用于表示时间跨度:间隔,期间和持续时间。


Trying to get Universal Time in java seems to be so difficult. Something like this in 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. 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

解决方案

tl;dr

Current moment in 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.

java.time

The modern approach to date-time handling in Java is the java.time classes.

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 instant = Instant.now();  // Current moment in UTC.

The Duration class represents a span of time unattached to the timeline. You can feed in a pair of Instant objects to calculate the number of days, hours, minutes, seconds, and nanoseconds in between.

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. For example, an hour and a half is 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. 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.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


Joda-Time

UPDATE: The Joda-Time project, now in maintenance mode, advises migration to java.time. Leaving this section intact for history.

Using the Joda-Time 2.4 library.

DateTime nowUtc = DateTime.now( DateTimeZone.UTC );

Best to avoid java.util.Date and .Calendar as they are notoriously troublesome. 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. Joda-Time offers 3 classes for representing a span of time: Interval, Period, and Duration.

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

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