How to get ordinal Weekdays in a Month

前端 未结 4 879
借酒劲吻你
借酒劲吻你 2020-12-22 02:21

hi i want to make a program in java where days,weekNo is parameter ..Like First Friday of the month or second Monday of the month ..and it returns the date

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 03:00

    tl;dr

    LocalDate firstFridayThisMonth =
        LocalDate.now( ZoneId.of( "America/Montreal" ) )
                 .with( TemporalAdjusters.firstInMonth( DayOfWeek.FRIDAY ) )
    

    Using java.time

    The other Answers are now outdated. The troublesome old date-time classes (Date, Calendar, etc.) are now legacy, supplanted by the java.time classes.

    LocalDate

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

    A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

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

    TemporalAdjuster

    The TemporalAdjuster interface provides for manipulating date-time values. The java.time classes use immutable objects, so the result is always a fresh new object with values based on the original.

    The TemporalAdjusters class (note plural name) provides several handy implementations. Amongst those are ones to get ordinal day-of-week within the month: firstInMonth(), lastInMonth(), and dayOfWeekInMonth(). All of these take an argument of a DayOfWeek enum object.

    LocalDate firstFridayOfThisMonth = 
        today.with(
            TemporalAdjusters.firstInMonth( DayOfWeek.FRIDAY ) 
        )
    ;
    

    …and…

    LocalDate secondMondayOfThisMonth = 
        today.with(
            TemporalAdjusters.dayOfWeekInMonth( 2 , DayOfWeek.MONDAY ) 
        )
    ;
    

    …and…

    LocalDate thirdWednesdayOfNextMonth = 
        today.plusMonths( 1 )
             .with(  
                 TemporalAdjusters.dayOfWeekInMonth( 3 , DayOfWeek.WEDNESDAY )  
             )
    ;
    

    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 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 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.

提交回复
热议问题