In Java, get all weekend dates in a given month

前端 未结 4 2165
野的像风
野的像风 2020-11-28 14:32

I need to find all the weekend dates for a given month and a given year.

Eg: For 01(month), 2010(year), the output should be : 2,3,9,10,16,17,23,24,30,31, all week

4条回答
  •  一个人的身影
    2020-11-28 15:13

    The Answer by Lokni appears to be correct, with bonus points for using Streams.

    EnumSet

    My suggestion for improvement: EnumSet. This class is an extremely efficient implementation of Set. Represented internally as bit vectors, they are fast to execute and taking very little memory.

    Using an EnumSet enables you to soft-code the definition of the weekend by passing in a Set.

    Set dows = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );
    

    Demo using the old-fashioned syntax without Streams. You could adapt Lokni’s answer’s code to use an EnumSet in a similar manner.

    YearMonth ym = YearMonth.of( 2016 , Month.JANUARY ) ;
    int initialCapacity = ( ( ym.lengthOfMonth() / 7 ) + 1 ) * dows.size() ;  // Maximum possible weeks * number of days per week.
    List dates = new ArrayList<>(  initialCapacity  );
    for (int dayOfMonth = 1;  dayOfMonth <= ym.lengthOfMonth() ;  dayOfMonth ++) {
        LocalDate ld =  ym.atDay( dayOfMonth ) ;
        DayOfWeek dow = ld.getDayOfWeek() ;
        if( dows.contains( dow ) ) {  
            // Is this date *is* one of the days we care about, collect it.
            dates.add( ld );
        }
    }
    

    TemporalAdjuster

    You can also make use of the TemporalAdjuster interface which provides for classes that manipulate date-time values. The TemporalAdjusters class (note the plural s) provides several handy implementations.

    The ThreeTen-Extra project provides classes working with java.time. This includes a TemporalAdjuster implementation, Temporals.nextWorkingDay().

    You can write your own implementation to do the opposite, a nextWeekendDay temporal adjuster.


    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.

    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 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.

提交回复
热议问题