我知道有很多关于如何获取的问题,但是我想和使用新的Java 8 Date api进行示例。我也知道JodaTime库,但是我想要一种没有外部库的工作方式。
功能需要抱怨以下限制:
- 防止日期保存错误
- 输入是两个Date对象(没有时间,我知道localdatetime,但是我需要处理日期实例)
DAYS.between(localDate1, localDate2)
。
我知道有很多关于如何获取的问题,但是我想和使用新的Java 8 Date api进行示例。我也知道JodaTime库,但是我想要一种没有外部库的工作方式。
功能需要抱怨以下限制:
DAYS.between(localDate1, localDate2)
。
Answers:
如果需要逻辑日历日,请使用以下DAYS.between()
方法java.time.temporal.ChronoUnit
:
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
有关更多信息,请参阅本文档。
Duration.between(today.atTime(0, 0), yesterday.atTime(0, 0)).toDays()
。
today.atTime(0, 0)
你可以做today.atStartOfDay()
。
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));
如果startDate和endDate是java.util.Date的实例
我们可以使用ChronoUnit 枚举中的between()方法:
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
//..
}
ChronoUnit.DAYS计算完成24小时的天数。
import java.time.temporal.ChronoUnit;
ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant());
//OR
ChronoUnit.DAYS.between(Instant.ofEpochMilli(startDate.getTime()), Instant.ofEpochMilli(endDate.getTime()));
在枚举java.time.temporal.ChronoUnit中使用DAYS 。下面是示例代码:
输出: *开始日期:2015-03-01和结束日期:2016-03-03之间的天数==>368。**开始日期:2016-03-03和结束日期之间的天数: 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);
}
}
干得好:
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);
}
}
获取圣诞节前一天的天数,试试看
System.out.println(ChronoUnit.DAYS.between(LocalDate.now(),LocalDate.of(Year.now().getValue(), Month.DECEMBER, 25)));
如果目标只是为了在几天之内获得差异,并且由于上述答案中提到了有关委托方法的内容,则要指出,一次也可以简单地使用-
public long daysInBetween(java.time.LocalDate startDate, java.time.LocalDate endDate) {
// Check for null values here
return endDate.toEpochDay() - startDate.toEpochDay();
}
使用最能满足您需求的类或方法:
持续时间使用基于时间的值(秒,纳秒)来衡量时间量。
期间使用基于日期的值(年,月,日)。
当您只想在单个时间单位(例如天或秒)中测量时间量时,ChronoUnit.between方法很有用。
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html