How do I get next Thursday from a random date?

前端 未结 3 1403
一整个雨季
一整个雨季 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);
    
    0 讨论(0)
  • 2021-01-13 09:08

    This formula will give you the Thursday following a date in A1

    =A1+7-WEEKDAY(A1+2)

    If A1 is a Thursday it returns that date, if it should be A1+7 if A1 is Thursday then change to this version

    =A1+8-WEEKDAY(A1+3)

    0 讨论(0)
  • 2021-01-13 09:11
    =IF(WEEKDAY(A1) < 5, A1+5-WEEKDAY(A1), A1+5+7-WEEKDAY(A1))
    

    Will give you the next Thursday from the date given in Cell A1 in Excel. If the input date is a Thursday, it will give the next Thursday.

    Use <= in the conditional if you want to display the input Thursday when given a Thursday, rather than displaying the next Thursday.

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