How to calculate the number of days in a period?
Asked Answered
P

5

76

For the following Period calculation:

Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 9, 2))

the result is:

P1M1D

This is equivalent to 31 days + 1 day = 32 days.

For this Period:

Period.between(LocalDate.of(2015, 8, 1), LocalDate.of(2015, 10, 2))

the result is:

P2M1D

This is equivalent to: 31 days (in August) + 30 days (in September) + 1 (in October) = 62 days

Is there a method in the java.time package which will give the number of days in a Period? I can't find one. Not sure if I have overlooked anything or if it is just plain not there.

Propst answered 14/6, 2015 at 19:27 Comment(2)
Period have a method long get(TemporalUnit unit), TemporalUnit can be ChronoUnit.DAYSNovick
@cyrbil but that does not do what the OP asks, try it and see.Brom
R
159

From the documentation:

To define an amount of time with date-based values (years, months, days), use the Period class. The Period class provides various get methods, such as getMonths, getDays, and getYears.To present the amount >of time measured in a single unit of time, such as days, you can use the ChronoUnit.between method.

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);
long p2 = ChronoUnit.DAYS.between(birthday, today);
System.out.println("You are " + p.getYears() + " years, " + p.getMonths() +
                   " months, and " + p.getDays() +
                   " days old. (" + p2 + " days total)");

The code produces output similar to the following:

You are 53 years, 4 months, and 29 days old. (19508 days total)
Refugiorefulgence answered 14/6, 2015 at 19:32 Comment(3)
Why didn't they put that method in Period itself?‮But
Thank you. I was using Period.getDays() without knowing that it returns only the days portion.Saprophagous
@But (1) Because months have different lengths, so a Period of, say, 1 month 1 day, doesn’t define a specific number of days. It could be 29, 30, 31 or 32. And (2) it’s exactly a quality of Period that it counts years, months and days rather than only days. You can have a period in a variable and add it to any LocalDate or other date-time object. If you insisted that a Period of 1 month 1 day should also define whether that means 29 or 32 days, then the addition would no longer make sense as a general thing to do.Bathy
B
30

There is no way to do what you ask. The reason is that it is not possible from a Period to deduce the actual number of calendar days in the period. A Period is not tied to specific dates, once constructed in the way you show, it loses track of the actual calendar dates.

For example your first period represents a period of 1 month and 1 day. But the period does not care which month. It is simply a concept of "a month and a day".

If you need the number of days between two dates you should use ChronoUnit.DAYS.between as Saket Mittal writes.

Brom answered 14/6, 2015 at 19:47 Comment(1)
Yes, I realised that. I just wanted to know how to recover the lost information so I could calculate the number of days. Thanks.Propst
O
13

There's a specific object depending at the amount of time you'd like to deal with. This page here is very useful explaining which is best for your scenario.

The ChronoUnit.between method is useful when you want to measure an amount of time in a single unit of time only, such as days or seconds

LocalDate localDateStartDate = LocalDate.of(2016, 06, 10);
LocalDate localDateEndDate = LocalDate.of(2016,06,23);
long days = ChronoUnit.DAYS.between(localDateStartDate, localDateEndDate);
Odessa answered 6/4, 2016 at 15:20 Comment(0)
D
1

ChronoUnit works fine, but it requires API level 26, if you are still targeting API level 24 like me, I found this alternative which works well:

val start = LocalDate.now()
val end = LocalDate.now().plusYears(1)
val totalDays = Duration.between(start.atStartOfDay(), end.atStartOfDay()).toDays()

atStartOfDay will convert LocalDate to LocalDateTime, with time set to 00:00, so we can use Duration calculation to get the total days between the dates

Disestablish answered 20/7, 2023 at 2:6 Comment(0)
D
1

You can just use LocalDate until method. For example:

LocalDate startDate = LocalDate.now().minusYears(1);
LocalDate endDate = LocalDate.now();

Long days = startDate.until(endDate, ChronoUnit.DAYS);
System.out.println(days);

This produces the following output:

365
Deploy answered 4/12, 2023 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.