The methods calculates the previous working day. - Java java.time

Java examples for java.time:Day

Description

The methods calculates the previous working day.

Demo Code


//package com.java2s;
import java.time.DayOfWeek;
import java.time.LocalDate;

import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class Main {
    /**//from w  w  w. jav a2 s .  c  o  m
     * 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);

        }
    }
}

Related Tutorials