两次之间如何获得差距? [英] How to get the difference without timezones between two times?

查看:85
本文介绍了两次之间如何获得差距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1970年前非常奇怪的日期中,我知道为什么两次看起来只有1s的差异,但有时我们可以得到另一个值。

In the question A very strange date before 1970, I know the reason why two times looks only have difference of 1s, but sometimes we can get another value.

例如从该问题:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    String str3 = "1927-12-31 23:54:07";  
    String str4 = "1927-12-31 23:54:08";  
    Date sDt3 = sf.parse(str3);  
    Date sDt4 = sf.parse(str4);  
    long ld3 = sDt3.getTime() /1000;  
    long ld4 = sDt4.getTime() /1000; 
    System.out.println(ld4-ld3);
}

输出将为 353 上海的时区,但在其他时区的 1

The output will be 353 in a timezone of Shanghai, but will be 1 in other timezones.

我的问题是,如何计算差分没有时区?如何总是得到正确的区别?

My question is, how to calculate the difference without timezones? How to always get the correct difference?

推荐答案

您可以轻松地处理时区, Joda时间

You can easily handle timezones with Joda Time.


  • 您可以创建一个<具有所需时区和使用的href =http://joda-time.sourceforge.net/api-release/org/joda/time/format/DateTimeFormatter.html =nofollow> DateTimeFormatter 它来解析字符串。 DateTime实例也可以是转换为其他时区

  • You can create a DateTimeFormatter with the desired time zone and use it to parse the string. The DateTime instances can also be converted to other time zones.

您可以使用 LocalDateTime 类。

此外, Period 类也有方法像 toStandardSeconds() toStandardDuration() normalizedStandard ()等,假设全周为7天,全天为24小时,所有时间为60分钟,所有分钟为60秒。夏令时是不正确的,对于一些不寻常的年表也可能不正确。但是,它被包含在内,因为它是许多应用程序和业务规则的有用操作。

Also the Period class has methods like toStandardSeconds(), toStandardDuration(), normalizedStandard() etc. which "makes the assumption that all weeks are 7 days, all days are 24 hours, all hours are 60 minutes and all minutes are 60 seconds. This is not true when daylight savings time is considered, and may also not be true for some unusual chronologies. However, it is included as it is a useful operation for many applications and business rules."

构建一个无时间的LocalDateTime,直接或通过从时区的DateTime实例转换。

Here is how to construct a timezoneless LocalDateTime either directly or by conversion from a timezoneful DateTime instance.

LocalDateTime a1 = new LocalDateTime(1927, 12, 31, 0, 0, 0, 0);
LocalDateTime a2 = new LocalDateTime(1928, 1, 2, 0, 0, 0, 0);
System.out.println(a1);
System.out.println(a2);
System.out.println(Seconds.secondsBetween(a1, a2).getSeconds()); // 172800 == 60 * 60 * 24 * 2 == 2 days without leap seconds

DateTime b1 = new DateTime(1927, 12, 31, 0, 0, 0, 0, DateTimeZone.forID("Asia/Shanghai"));
DateTime b2 = new DateTime(1928, 1, 2, 0, 0, 0, 0, DateTimeZone.forID("Asia/Shanghai"));
System.out.println(b1);
System.out.println(b2);
System.out.println(Seconds.secondsBetween(b1, b2).getSeconds());

LocalDateTime c1 = b1.toLocalDateTime();
LocalDateTime c2 = b2.toLocalDateTime();
System.out.println(c1);
System.out.println(c2);
System.out.println(Seconds.secondsBetween(c1, c2).getSeconds());

这将打印如下。请注意上海时间之前的有趣的时区,之后其1927年的时区调整

This will print as follows. Notice the funny timezone in Shanghai time before its timezone adjustment in 1927.

1927-12-31T00:00:00.000
1928-01-02T00:00:00.000
172800
1927-12-31T00:00:00.000+08:05:52
1928-01-02T00:00:00.000+08:00
173152
1927-12-31T00:00:00.000
1928-01-02T00:00:00.000
172800

这篇关于两次之间如何获得差距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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