How do you subtract Dates in Java?
This question already has an answer here:
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 / ThreeTen which is (most likely) going to be included in the upcoming Java 8.
Until then, you can get away with 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.
您可以使用以下方法:
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)));
If you deal with dates it is a good idea to look at the joda time library for a more sane Date manipulation model.
http://joda-time.sourceforge.net/
链接地址: http://www.djcxy.com/p/18594.html上一篇: 从现在开始,如何计算Joda的流逝时间
下一篇: 你如何减去Java中的日期?