How do I get next Thursday from a random date?

前端 未结 3 1401
一整个雨季
一整个雨季 2021-01-13 08:45

What I want to do is so you have a list of random dates, it will never be the same so it has to be a universal formula, but you get any date, any date of the year and then e

3条回答
  •  北海茫月
    2021-01-13 08:55

    For new Java 8 Time API, you can use below code. You need to change DayOfWeek.THURSDAY etc in the below code.

    LocalDate date1=LocalDate.now();
    // This will give you int value of Day(for eg. Friday) and the date you want to check for
    
    int duration=DayOfWeek.WEDNESDAY.getValue()-date1.getDayOfWeek().getValue();
    System.out.println("duration "+duration);
    if(date1.getDayOfWeek().equals(DayOfWeek.WEDNESDAY)) {
        System.out.println("Wednesday");
    } else {
        date1=date1.plusDays(7).minusDays(7-  duration); 
    }
    // it will first give next day of week then minus the duration from 7.
    
    System.out.println("new date1 "+date1);
    

提交回复
热议问题