Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the number of days between LocalDate java? [duplicate]

Tags:

java

java-8

I've got two LocalDates:

LocalDate date1;
LocalDate date2;
//...

How to find the number of days between those dates?

like image 520
user3663882 Avatar asked Jul 08 '16 17:07

user3663882


People also ask

How do I get the number of days between two dates in Java?

The Period class has a between() method - just as the previously discussed ChronoUnit . This method takes in two LocalDate objects, one representing the starting date, and the second being the end date. It returns a Period consisting of the number of years, months, and days between two dates.

How do I compare dates with LocalDate?

LocalDate compareTo() Method The method compareTo() compares two instances for the date-based values (day, month, year) and returns an integer value based on the comparison. 0 (Zero) if both the dates represent the same date in calendar. Positive integer if given date is latter than the otherDate.

How do you find the difference between two LocalDateTime?

Here's a way to calculate the difference although not necessarily the fastest: LocalDateTime fromDateTime = LocalDateTime. of(1984, 12, 16, 7, 45, 55); LocalDateTime toDateTime = LocalDateTime. of(2014, 9, 10, 6, 40, 45); LocalDateTime tempDateTime = LocalDateTime.

How do you check if a date is within a range in Java?

We can use the simple isBefore , isAfter and isEqual to check if a date is within a certain date range; for example, the below program check if a LocalDate is within the January of 2020. startDate : 2020-01-01 endDate : 2020-01-31 testDate : 2020-01-01 testDate is within the date range.


1 Answers

I would do something like

long daysBetween = DAYS.between(date1, date2);
like image 83
mattias Avatar answered Oct 11 '22 09:10

mattias