This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
Problem: Java 8 calculates the number of days between two dates
I know there are a lot of questions about how to get Dates in Java, but I’d like a new example using the Java 8 Date API. I also knew about the JodaTime library, but I wanted an approach that didn’t rely on external libraries. 2. Enter two Date objects (I don’t have time to learn about LocalDateTime, but I need to do this for Date instances).
Answer a
If need logical calendar date, please use the Java. Time. Temporal. DAYS of ChronoUnit. Between () method:
LocalDate dateBefore;
LocalDate dateAfter;
long daysBetween = DAYS.between(dateBefore, dateAfter);
Copy the code
If you want to use 24 hours, you can use the Duration class instead:
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
Copy the code
Refer to the documentation for more information
Answer two
You can use 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));
Copy the code
Answer three
If startDate and endDate are instances of java.util.Date
We can use the between () method in the ChronoUnit enumeration:
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
/ /..
}
Copy the code
ChronoUnit.DAYS Every 24 hours is counted as a day.
import java.time.temporal.ChronoUnit;
ChronoUnit.DAYS.between(startDate.toInstant(), endDate.toInstant());
//OR
ChronoUnit.DAYS.between(Instant.ofEpochMilli(startDate.getTime()), Instant.ofEpochMilli(endDate.getTime()));
Copy the code
Answer four
In the enumerated type Java. Time. Temporal. ChronoUnit used in DAYS. Here is the sample code:
*Number of days between the start date: 2015-03-01 and end date: 2016-03-03 is ==> 368. *Number of days between the start date : 2016-03-03 and end date : 2015-03-01 is ==> -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); }}Copy the code
The article translated from Stack Overflow:stackoverflow.com/questions/2…