1

I have 2 dates like this :

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);

TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

It is easy to know the total days between, but I'm not familiar at all with the API and would like to know if there is an easier way to find the number of days in each months between 2 dates without loops and ifs.

Example :

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);

Would give 31 for January and 2 for February.

Thanks!

Benoit
  • 363
  • 6
  • 23
  • 2
    If you want to iterate over every month betweeen start and end in order to determine the day count per month then you will probably need any kind of looping. Hard to avoid. It is the nature of your task/problem. – Meno Hochschild Jun 03 '15 at 20:21
  • For a modern solution using *java.time* classes and lambda syntax, see [this Answer](https://stackoverflow.com/a/59514717/642706) to a similar question. `Map < YearMonth, Long > map = start .datesUntil( stop ) .collect( Collectors.groupingBy( ( LocalDate localDate ) -> YearMonth.from( localDate ) , TreeMap::new , Collectors.counting() ) );` – Basil Bourque Dec 29 '19 at 00:06

4 Answers4

0

I did it with a loop finally.

DateTime startingDate = new DateTime(STARTING_YEAR, STARTING_MONTH, STARTING_DAY, 0, 0);
DateTime endingDate = new DateTime(ENDING_YEAR, ENDING_MONTH, ENDING_DAY, 0, 0);
TOTAL_DAYS = Days.daysBetween(startingDate, endingDate).getDays();

DateTime currentDate = startingDate;

System.out.println(currentDate.dayOfMonth().getMaximumValue() - currentDate.dayOfMonth().get() + 1);
currentDate = currentDate.plus(Period.months(1));

while (currentDate.isBefore(endingDate)) { 
     System.out.println(currentDate.dayOfMonth().getMaximumValue());
     currentDate = currentDate.plus(Period.months(1));
 }  
System.out.println(endingDate.dayOfMonth().get());
Benoit
  • 363
  • 6
  • 23
0

double days = (endingDate.getMillis()-startingDate.getMillis())/86400000.0;

that gives the number of days as a floating point number. truncate if you only want the number of full days.

scott
  • 1
0

This may help:

DateTime startingDate = new DateTime(2000, 1, 1, 0, 0);
DateTime endingDate = new DateTime(2000, 2, 3, 0, 0);

Duration duration = new Duration(startingDate, endingDate);

System.out.println(duration.getStandardDays());//get the difference in number of days
optimistic_creeper
  • 2,739
  • 3
  • 23
  • 37
0

FYI, the Joda-Time project, now in maintenance mode, advises migration to java.time.

java.time

You will need to iterate if you want to address each intervening month individually. But this job is somewhat simplified by the YearMonth class. Furthermore, you can mask away the iteration by using Streams.

Half-Open

The java.time classes wisely use the Half-Open approach to defining a span of time. This means the beginning is inclusive while the ending is exclusive. So a range of months needs to end with the month following the ending target month.

TemporalAdjuster

The TemporalAdjuster interface provides for manipulation of date-time values. The TemporalAdjusters class (note the plural s) provides several handy implementations. Here we need:

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate startDate = LocalDate.of( 2000 , 1 , 1 );
YearMonth ymStart = YearMonth.from( startDate );

LocalDate stopDate = LocalDate.of( 2000 , 2 , 3 );
LocalDate stopDateNextMonth = stopDate.with( TemporalAdjusters.firstDayOfNextMonth() );
YearMonth ymStop = YearMonth.from( stopDateNextMonth );

Loop each month in between.

You can ask for a localized name of the month, by the way, via the Month enum object.

YearMonth ym = ymStart;
do {
    int daysInMonth = ym.lengthOfMonth ();
    String monthName = ym.getMonth ().getDisplayName ( TextStyle.FULL , Locale.CANADA_FRENCH );

    System.out.println ( ym + " : " + daysInMonth + " jours en " + monthName );

    // Prepare for next loop.
    ym = ym.plusMonths ( 1 );
} while ( ym.isBefore ( ymStop ) );

2000-01 : 31 jours en janvier

2000-02 : 29 jours en février


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154