On a specific date, define the nth day of the week in the month, then define the same in the next 6 months, in Java

During a given date, for example 2018-03-05, how to determine that this is the first Monday of this month?

And after determining this fact, how to calculate the same n-th day of the week for a month over the next six months? For example, on Monday 2018-04-02 and on Monday 2018-05-03.

Just like this question , but using Java.

I tried this code but not getting the expected results. What I expect:

[2018-04-02, 2018-05-07, 2018-06-04, 2018-07-02, 2018-08-06, 2018-09-03]

Perhaps I misunderstand ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH.

LocalDate ld = LocalDate.parse( "2018-03-05" );
int alignedDayOfWeekInMonth = ld.get( ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH );
DayOfWeek dayOfWeek = ld.getDayOfWeek();
YearMonth ym = YearMonth.from( ld );

int countMonths = 6;
List < LocalDate > localDates = new ArrayList <>( countMonths );
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth( alignedDayOfWeekInMonth , dayOfWeek );
for ( int i = 1 ; i <= countMonths ; i++ ) {
    LocalDate firstOfMonth = ym.plusMonths( i ).atDay( 1 );
    LocalDate localDate = firstOfMonth.with( ta );
    localDates.add( localDate );
}

Dump for the console.

System.out.println( "ld.toString(): " + ld );
System.out.println( "alignedDayOfWeekInMonth: " + alignedDayOfWeekInMonth );
System.out.println( "dayOfWeek.toString(): " + dayOfWeek );
System.out.println("ym.toString(): " + ym);
System.out.println( "localDates: " + localDates );

At startup.

ld.toString(): 2018-03-05
alignedDayOfWeekInMonth: 5
dayOfWeek.toString(): MONDAY
ym.toString(): 2018-03
localDates: [2018-04-30, 2018-06-04, 2018-07-02, 2018-07-30, 2018-09-03, 2018-10-01]

, , java.time: , n- Java

+4
3

TemporalAdjusters.dayOfWeekInMonth(int ordinal, DayOfWeek dayOfWeek), ordinal - , ALIGNED_DAY_OF_WEEK_IN_MONTH ΒΉ.

java.time?:
WeekFields, WeekFields.ISO.
ChronoField WeekFields.ISO.weekOfMonth();.


:

LocalDate ld = LocalDate.parse( "2018-03-05" );
DayOfWeek dayOfWeek = ld.getDayOfWeek();
int ordinal = ld.get(WeekFields.of(dayOfWeek, 7).weekOfMonth());
YearMonth ym = YearMonth.from( ld );

int countMonths = 6;
List < LocalDate > localDates = new ArrayList <>( countMonths );
TemporalAdjuster ta = TemporalAdjusters.dayOfWeekInMonth( ordinal, dayOfWeek );
for ( int i = 1 ; i <= countMonths ; i++ ) {
    LocalDate firstOfMonth = ym.plusMonths( i ).atDay( 1 );
    LocalDate localDate = firstOfMonth.with( ta );
    localDates.add( localDate );
}

ΒΉ: ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH - n- , DAY_OF_WEEK, , .

+1

:

    int alignedWeekOfMonth = ld.get(ChronoField.ALIGNED_WEEK_OF_MONTH);

alignedDayOfWeekInMonth, , , . :

ld.toString(): 2018-03-05
alignedWeekOfMonth: 1
dayOfWeek.toString(): MONDAY
ym.toString(): 2018-03
localDates: [2018-04-02, 2018-05-07, 2018-06-04, 2018-07-02, 2018-08-06, 2018-09-03]

, , .

, , ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH. , 5 - 5 1st. chrono, , 5, 1. 1, ChronoField.ALIGNED_WEEK_OF_MONTH . .

+1

, :)

public static void main(String[] args) {    
    LocalDate ld = LocalDate.parse( "2018-03-04" );
    DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;

    LocalDate first = ld.withDayOfMonth(1);

    List<LocalDate> firstDays = new ArrayList<>();
    //I deliberately included the start date in the loop. 
    //Easily fixed if it shouldn't be included
    for (int i = 0; i <= 6; i++) {
        LocalDate firstDay = getFirstDay(first.plusMonths(i), dayOfWeek);
        firstDays.add(firstDay);
    }
}

static LocalDate getFirstDay(LocalDate date, DayOfWeek searchedDay) {
    LocalDate temp = date;
    int i = 0;
    while (temp.getDayOfWeek() != searchedDay) {
         temp = date.plusDays(i++);
    }
    return temp;
}
0

Source: https://habr.com/ru/post/1696343/


All Articles