Java,如何在考虑DST时减去Date对象 [英] Java, How to subtract Date objects whilst considering DST

查看:107
本文介绍了Java,如何在考虑DST时减去Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码用于计算两个Date对象之间的天数,在大多数情况下,它可以正常工作。
但是,如果两个对象之间的日期范围包括3月底,结果总是比它应该少1个。例如2014年3月31日 - 2014年3月29日= 1,而应为2。



我知道这是因为3月有30天24小时, 1天23小时,由于DST,这是值为1的原因。



但是,我不知道最好的方法来解释缺少的时间

  //这是我最初的
int numDays =(int)((dateTo.getTime() - dateFrom.getTime())/(1000 * 60 * 60 * 24));

//我已经尝试了四舍五入,因为它应该剩下23个小时,但实际上并没有起作用。
int numDays =(Math.round(dateTo.getTime() - dateFrom.getTime())/(1000 * 60 * 60 * 24));

任何帮助/指针都将不胜感激。



我不得不使用Java 7,我不能使用Jodatime。

解决方案

你的第二个例子是很接近。您的圆括号 Math.round()仅围绕减法,因为这已经是一个整数(好吧,一个 long 真的),没有什么发生,然后你分裂。你的第二位代码的另一个问题是你正在做整数除法,它总是在小数点后截断零件。尝试这样:

  long numDays2 = Math.round((dateTo.getTime() -  dateFrom.getTime())/(1000.0 * 60 * 60 * 24)); 

(如所示,我更改了 Math.round()括号,并通过使除数为 double 作为浮点除法。)



如所示通过评论,虽然这是一个黑客。特别是,它会告诉你,3月5日上午6点到3月6日下午8点之间有两天。这可能不是你想要的。尝试这个大小代替:

  SimpleDateFormat fmt = new SimpleDateFormat(yyyy-MM-dd); 
日历cal = Calendar.getInstance();
cal.setTime(fmt.parse(2014-03-29));
long start = cal.getTimeInMillis();
start + = cal.getTimeZone()。getOffset(start);
cal.setTime(fmt.parse(2014-03-31));
long end = cal.getTimeInMillis();
end + = cal.getTimeZone()。getOffset(end);

System.out.println((end-start)/86400000.0);

难道是丑吗?是。是怪异吗是。它工作吗是的,我想是这样)。注意,我提供了一个 double ;您可以对此结果应用任何四舍五入。


I have a piece of code that is used to calculate the number of days between two Date objects, and in most instances, it works fine. However, if the date range between the two objects includes the end of March, the result is always 1 less than it should be. e.g March 31 2014 - March 29 2014 = 1, whereas it should be 2.

I understand that this is due to the fact that March has 30 days of 24 hours and 1 day of 23 hours due to DST, which is the cause of the value being 1 less.

However, I am not sure the best way to account for the missing hour.

   // This was what I have initially
    int numDays = (int) ((dateTo.getTime() - dateFrom.getTime()) / (1000 * 60 * 60 * 24));

   // I have tried rounding, since it should have 23 hours left over, but it didn't actually work.
    int numDays = (Math.round(dateTo.getTime() - dateFrom.getTime()) / (1000 * 60 * 60 * 24));

Any help/pointers would be greatly appreciated.

I am and have to use Java 7 and I am not able to use Jodatime unfortunately.

解决方案

Your second example is very close. Your parentheses for Math.round() only surround the subtraction, though, so since that's already an integer (well, a long really), nothing happens, and then you divide. The other problem with your second bit of code is that you are doing integer division which always truncates the part after the decimal point. Try this:

long numDays2 = Math.round((dateTo.getTime() - dateFrom.getTime()) / (1000.0 * 60 * 60 * 24));

(As indicated, I changed the Math.round() parens, and made it floating point division by making the divisor a double.)

As indicated by the comments, though, this is a hack. In particular, it will tell you that there are two days between 6AM March 5 and 8PM March 6. It's probably not really what you want. Try this on for size instead:

    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(fmt.parse("2014-03-29"));
    long start = cal.getTimeInMillis();
    start += cal.getTimeZone().getOffset(start);
    cal.setTime(fmt.parse("2014-03-31"));
    long end = cal.getTimeInMillis();
    end += cal.getTimeZone().getOffset(end);

    System.out.println((end - start)/86400000.0);

Is it ugly? Yes. Is it weird? Yes. Does it work? Yes (I think so). Note that I'm providing a double as a result; you can apply any rounding you want to this result.

这篇关于Java,如何在考虑DST时减去Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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