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

This question already has answers here:
Calculate days between two Dates in Java 8
(14 answers)
Closed 6 years ago.
I've got two LocalDates:
LocalDate date1;
LocalDate date2;
//...
How to find the number of days between those dates?

LocalDate.until is what you're looking for. (LocalDate implements Temporal, which is what Days accepts, and ChronoUnit is a library of TemporalUnit instances.)
long days = date1.until(date2, ChronoUnit.DAYS);

I would do something like
long daysBetween = DAYS.between(date1, date2);

Related

How to calculate remaining days of the month? [duplicate]

This question already has answers here:
How to get number of days between today's date and last date of the current month? [closed]
(3 answers)
How to calculate the number of days in a period?
(3 answers)
Closed 2 years ago.
How do I calculate how many days are left in the month we are in, in Java?
For example, today is the 5th of November, the result would be:
25 days left for the end of the month.
How to do this?
In Java there's been for a while Java Time API, which allows to get the number of days in the given month and calculate the difference using java.time.temporal.ChronoUnit.DAYS:
LocalDate today = LocalDate.now();
LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth());
long daysBetween = DAYS.between(today, endOfMonth);

How to calculate Date days difference, and not by seconds? [duplicate]

This question already has answers here:
getting the difference between date in days in java [duplicate]
(3 answers)
Java, Calculate the number of days between two dates [duplicate]
(10 answers)
Java 8 Day Difference without the Time Component
(1 answer)
Remaining days to a date is not showing correctly
(4 answers)
Closed 4 years ago.
For example i have two dates:
2018-11-30 18:00:00
2018-12-01 00:00:00
How you can see, that less then 24 hours difference, but I need to recognize that a one day of differnce, and i can't just subtract, cause of month change.
In output i need to return int count of days.
In c# it will be just:
(EndDate - StartDate).TotalDays
What Java code will be similar?
I searched somewhere before I asked, but that didn’t solve the problem. Here’s my attempt after searching:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime d1 = LocalDateTime.parse("2018-11-30 18:00:00", dtf);
LocalDateTime d2 = LocalDateTime.parse("2018-12-01 00:00:00", dtf);
long days = ChronoUnit.DAYS.between(d1, d2);
System.out.println("Days: " + days); // Days: 0
I wish to get 1 day of difference.

Make operation on a date in Java [duplicate]

This question already has answers here:
How do I do calendar arithmetic with java.util.Date?
(6 answers)
Closed 4 years ago.
What is the best way to make operation on a util.date Object in java.
For example:
I have a date 2018.10.02 and i want to add 3 Month to this date and to get another util.date object with the good date.
The same with adding or substracting day, years or hours...
Thanks
You can use java.time package since java8, for example:
Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
zonedDateTime = zonedDateTime.plusMonths(3);
Date afterThreeMonth = Date.from(zonedDateTime.toInstant())
New class java.time.LocalDate is better for that kind of operations:
LocalDate date = LocalDate.parse("2018-10-02").plusMonths(3);

Calculating number of months between two OffsetDateTimes [duplicate]

This question already has answers here:
Java 8: Calculate difference between two ZonedDateTime
(3 answers)
Closed 4 years ago.
I am using java 8 and I'm trying to calculate the amount of months between two OffsetDateTime objects. What is the best way to do this?
Without more details, the standard way would be:
long months = ChronoUnit.MONTHS.between(odt1, odt2);
the most comprehensible way (IMO) is to use ChronoUnit
OffsetDateTime odt1 = OffsetDateTime.now();
OffsetDateTime odt2 = odt1.plusMonths(10);
System.out.println(ChronoUnit.MONTHS.between(odt1, odt2));

How to force java.time.LocalDate to assume 19th century as YY year pattern? [duplicate]

This question already has answers here:
Parsing string to local date doesn't use desired century
(1 answer)
How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?
(1 answer)
Closed 6 years ago.
The following test fails: a '86 birthday is formated as 2068. How can I format is as 1986?
#Test
public void testBirthday() {
assertEquals("1986-08-07", java.time.LocalDate.parse("070886",
java.time.format.DateTimeFormatter.ofPattern("ddMMyy")));
}
Fails with:
java.lang.AssertionError: expected:<1986-08-07> but was:<2086-08-07>
This is much different to org.joda.time library which would correctly assume 19' here.
/Sidenote: regarding the marked answers in the "duplicate" questions, I don't think this is a duplicate!
The base year can be controlled using DateTimeFormatterBuilder.appendValueReduced().
This code would parse with a base date of 1900 rather than 2000:
DateTimeFormatter f = new DateTimeFormatterBuilder()
.appendPattern("ddMM")
.appendValueReduced(ChronoField.YEAR, 2, 2, 1900)
.toFormatter();
LocalDate date = LocalDate.parse("070886", f);
Java 8's LocalDate class uses 2000+ for the year by default. In order to parse a yy format for 1900 and up, you'll need to reduce the time yourself.
Of course, even if you check the date against the current year and subtract 100 years if its after today, this fix won't work for people over 100 years old.
You can also use a yyyy format. Or just make use of a Date object instead, and convert it to a LocalDate if you wish.

Categories