What would be a good implementation to get all Monday and Thursday dates Between a given date range (DateX and DateY) in JAVA

后端 未结 3 685
执笔经年
执笔经年 2021-01-21 09:28

I want to get some days between a given range of dates. What would be an optimal solution for this? For example, I want to get all Monday, Wednesday and Thursday dates from toda

相关标签:
3条回答
  • 2021-01-21 09:49

    Calculate the number of weeks between x and y, and take x's weekday into account.

    0 讨论(0)
  • 2021-01-21 09:52

    Using java.time

    The modern approach uses the java.time classes.

    Instantiate List objects to collect your results.

    List<LocalDate> mondays = new ArrayList<>() ;
    List<LocalDate> wednesdays = new ArrayList<>() ;
    List<LocalDate> thursdays = new ArrayList<>() ;
    

    Determine today's date. This requires a time zone, as the date varies around the globe by zone for any given moment.

    LocalDate today = LocalDate.now( ZoneId.of( "America/Montreal" ) ) ;
    

    Add two more months. The java.time classes smartly handle the issue of shorter/longer months. Read the class doc to see if you agree with their behavior/policies.

    LocalDate twoMonthsLater = today.plusMonths( 2 );
    

    Loop each date, incrementing one day at a time. Test its day-of-week value using the DayOfWeek enum. Note that you can switch on an Enum object, but oddly cannot include its class name prefix. So we use MONDAY rather than my preferred DayOfWeek.MONDAY.

    LocalDate localDate = today ;
    while ( localDate.isBefore( twoMonthsLater ) ) {
        DayOfWeek dow = localDate.getDayOfWeek() ;
         switch ( dow ) {
                case MONDAY: 
                    mondays.add( localDate ) ;
                    break;
    
                case WEDNESDAY: 
                    wednesdays.add( localDate ) ;
                    break;
    
                case THURSDAY:
                    thursdays.add( localDate ) ;
                    break;
    
                default: 
                    // Ignore any other day-of-week.
                    break;
            }
            // Set-up the next loop. Increment by one day at a time.
            localDate = localDate.plusDays( 1 ) ;
    }
    

    Dump to console.

    System.out.println( "From " + today + " to " + twoMonthsLater + ":"  );
    System.out.println( "mondays: " + mondays ) ;
    System.out.println( "wednesdays: " + wednesdays ) ;
    System.out.println( "thursdays: " + thursdays ) ;
    

    See this code run live at IdeOne.com.

    From 2017-10-17 to 2017-12-17:

    mondays: [2017-10-23, 2017-10-30, 2017-11-06, 2017-11-13, 2017-11-20, 2017-11-27, 2017-12-04, 2017-12-11]

    wednesdays: [2017-10-18, 2017-10-25, 2017-11-01, 2017-11-08, 2017-11-15, 2017-11-22, 2017-11-29, 2017-12-06, 2017-12-13]

    thursdays: [2017-10-19, 2017-10-26, 2017-11-02, 2017-11-09, 2017-11-16, 2017-11-23, 2017-11-30, 2017-12-07, 2017-12-14]


    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, & SimpleDateFormat.

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

    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, Java 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 Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    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.

    0 讨论(0)
  • 2021-01-21 09:56

    Have a look at java.util.Calendar. One can get a instance of it, set it to a given date, then, for example, call get(DAY_OF_WEEK) on it. Also it has some nice methods to get number of day in a given year and number of days in a given year.

    0 讨论(0)
提交回复
热议问题