计算Java 8中两个date之间的天数
我知道有很多关于如何获得的问题,但是我想要和使用新的Java 8 Date api的例子。 我也知道JodaTime库,但我想要一个没有外部库的工作方式。
function需要抱怨这些限制:
- 防止date保存时间的错误
- input是两个date的对象(没有时间,我知道localdatetime,但我需要做的date实例)
如果您想要逻辑日历日 ,请使用DAYS.between()
方法:
LocalDate dateBefore; LocalDate dateAfter; long daysBetween = DAYS.between(dateBefore, dateAfter);
如果你想要24小时的文字 ( 持续时间 ),你可以使用Duration
类:
LocalDate today = LocalDate.now() LocalDate yesterday = today.minusDays(1); // Duration oneDay = Duration.between(today, yesterday); // throws an exception Duration.between(today.atStartOfDay(), yesterday.atStartOfDay()).toDays() // another option
有关更多信息,请参阅此文档 (以及其他文档:Oracle的Java 8)。
基于VGR的评论这里是你可以使用:
ChronoUnit.DAYS.between(firstDate, secondDate)
你可以使用until()
:
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4); LocalDate christmas = LocalDate.of(2014, Month.DECEMBER, 25); System.out.println("Until christmas: " + independenceDay.until(christmas)); System.out.println("Until christmas (with crono): " + independenceDay.until(christmas, ChronoUnit.DAYS));
你可以使用DAYS.between
的java.time.temporal.ChronoUnit
例如
import java.time.temporal.ChronoUnit; public long getDaysCountBetweenDates(LocalDate dateBefore, LocalDate dateAfter) { return DAYS.between(dateBefore, dateAfter); }
干得好:
public class DemoDate { public static void main(String[] args) { LocalDate today = LocalDate.now(); System.out.println("Current date: " + today); //add 1 month to the current date LocalDate date2 = today.plus(1, ChronoUnit.MONTHS); System.out.println("Next month: " + date2); // Put latest date 1st and old date 2nd in 'between' method to get -ve date difference long daysNegative = ChronoUnit.DAYS.between(date2, today); System.out.println("Days : "+daysNegative); // Put old date 1st and new date 2nd in 'between' method to get +ve date difference long datePositive = ChronoUnit.DAYS.between(today, date2); System.out.println("Days : "+datePositive); } }
在枚举java.time.temporal.ChronoUnit中使用DAYS。 以下是示例代码:
输出: *从开始date:2015-03-01到结束date:2016-03-03之间的天数是==> 368. **开始date之间的天数:2016-03-03和结束date之间的天数: 2015-03-01是==> -368 *
package com.bitiknow.date; import java.time.LocalDate; import java.time.temporal.ChronoUnit; /** * * @author pradeep * */ public class LocalDateTimeTry { public static void main(String[] args) { // Date in String format. String dateString = "2015-03-01"; // Converting date to Java8 Local date LocalDate startDate = LocalDate.parse(dateString); LocalDate endtDate = LocalDate.now(); // Range = End date - Start date Long range = ChronoUnit.DAYS.between(startDate, endtDate); System.out.println("Number of days between the start date : " + dateString + " and end date : " + endtDate + " is ==> " + range); range = ChronoUnit.DAYS.between(endtDate, startDate); System.out.println("Number of days between the start date : " + endtDate + " and end date : " + dateString + " is ==> " + range); } }
从今天开始圣诞节前的天数,试试这个
System.out.println(ChronoUnit.DAYS.between(LocalDate.now(),LocalDate.of(Year.now().getValue(), Month.DECEMBER, 25)));