Java Data Type How to - Get previous working day and recognize Saturday and Sunday as non-working days








Question

We would like to know how to get previous working day and recognize Saturday and Sunday as non-working days.

Answer

   // w  ww. j  a  v a 2 s . co  m
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class Main {
  public static void main(String[] argv) {
    System.out.println(getPreviousWorkingDay(LocalDate.now()));
  }

  /**
   * The methods calculates the previous working day. It only recognize Saturday
   * and Sunday as non -working days.
   * 
   * @param date
   *          Date as starting point for the calculation, cannot be null
   * @return The previous working day
   */
  public static LocalDate getPreviousWorkingDay(LocalDate date) {
    DayOfWeek dayOfWeek = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));
    switch (dayOfWeek) {
    case MONDAY:
      return date.minus(3, ChronoUnit.DAYS);
    case SUNDAY:
      return date.minus(2, ChronoUnit.DAYS);
    default:
      return date.minus(1, ChronoUnit.DAYS);

    }
  }

}

The code above generates the following result.