你如何在 Java 中减去日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3526485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you subtract Dates in Java?
提问by Haoest
My heart is bleeding internally after having to go so deep to subtract two dates to calculate the span in number of days:
在不得不如此深入地减去两个日期来计算天数的跨度之后,我的心在内部流血:
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c2 = new GregorianCalendar();
c1.set(2000, 1, 1);
c2.set(2010,1, 1);
long span = c2.getTimeInMillis() - c1.getTimeInMillis();
GregorianCalendar c3 = new GregorianCalendar();
c3.setTimeInMillis(span);
long numberOfMSInADay = 1000*60*60*24;
System.out.println(c3.getTimeInMillis() / numberOfMSInADay); //3653
where it's only 2 lines of code in .NET, or any modern language you name.
在 .NET 或您命名的任何现代语言中只有 2 行代码。
Is this atrocious of java? Or is there a hidden method I should know?
这是java的残暴吗?或者是否有我应该知道的隐藏方法?
Instead of using GregorianCalendar, is it okay to use Date class in util? If so, should I watch out for subtle things like the year 1970?
除了使用 GregorianCalendar,在 util 中使用 Date 类可以吗?如果是这样,我应该注意像 1970 年这样的微妙事物吗?
Thanks
谢谢
采纳答案by BalusC
It's indeed one of the biggest epic failures in the standard Java API. Have a bit of patience, then you'll get your solution in flavor of the new Date and Time API specified by JSR 310 / ThreeTenwhich is (most likely) going to be included in the upcoming Java 8.
这确实是标准 Java API 中最大的史诗般的失败之一。有一点耐心,然后您将获得具有JSR 310 / ThreeTen指定的新日期和时间 API 风味的解决方案,它(很可能)将包含在即将推出的 Java 8 中。
Until then, you can get away with JodaTime.
在此之前,您可以使用JodaTime。
DateTime dt1 = new DateTime(2000, 1, 1, 0, 0, 0, 0);
DateTime dt2 = new DateTime(2010, 1, 1, 0, 0, 0, 0);
int days = Days.daysBetween(dt1, dt2).getDays();
Its creator, Stephen Colebourne, is by the way the guy behind JSR 310, so it'll look much similar.
顺便说一下,它的创建者 Stephen Colebourne 是 JSR 310 的幕后推手,所以它看起来非常相似。
回答by Peter Tillemans
If you deal with dates it is a good idea to look at the joda time library for a more sane Date manipulation model.
如果您处理日期,最好查看 joda 时间库以获得更合理的日期操作模型。
回答by Strom
Well you can remove the third calendar instance.
那么您可以删除第三个日历实例。
GregorianCalendar c1 = new GregorianCalendar();
GregorianCalendar c2 = new GregorianCalendar();
c1.set(2000, 1, 1);
c2.set(2010,1, 1);
c2.add(GregorianCalendar.MILLISECOND, -1 * c1.getTimeInMillis());
回答by daniel.deng
You can use the following approach:
您可以使用以下方法:
SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd");
long d1=formater.parse("2001-1-1").getTime();
long d2=formater.parse("2001-1-2").getTime();
System.out.println(Math.abs((d1-d2)/(1000*60*60*24)));
回答by Luna Kong
Here's the basic approach,
这是基本方法,
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = dateFormat.parse("2013-11-29");
Date endDate = dateFormat.parse("2013-12-4");
Calendar beginCalendar = Calendar.getInstance();
beginCalendar.setTime(beginDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
There is simple way to implement it. We can use Calendar.add method with loop. The minus days between beginDate and endDate, and the implemented code as below,
有一个简单的方法来实现它。我们可以在循环中使用 Calendar.add 方法。beginDate 和 endDate 之间的负天数,以及实现的代码如下,
int minusDays = 0;
while (true) {
minusDays++;
// Day increasing by 1
beginCalendar.add(Calendar.DAY_OF_MONTH, 1);
if (dateFormat.format(beginCalendar.getTime()).
equals(dateFormat.format(endCalendar).getTime())) {
break;
}
}
System.out.println("The subtraction between two days is " + (minusDays + 1));**