Java DayOfWeek calculate days from last Friday

Description

Java DayOfWeek calculate days from last Friday

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Main {
  public static void main(String[] argv) {
    LocalDate date = LocalDate.now();
    int d = daysFromLastFriday(date);
    System.out.println(date);/*from   w  w  w. j a  va2  s.c o m*/
    System.out.println(d);

  }

  public static int daysFromLastFriday(LocalDate date) {
    int dateDayOfWeek = date.getDayOfWeek().getValue();
    int fridayDayOfWeek = DayOfWeek.FRIDAY.getValue();

    if (dateDayOfWeek > fridayDayOfWeek) {
      return dateDayOfWeek - fridayDayOfWeek;
    } else {
      return 7 - (fridayDayOfWeek - dateDayOfWeek);
    }
  }
}



PreviousNext

Related