TL; DR
LocalDate.now ( ZoneId.of ( "America/Montreal" ) ) .with ( org.threeten.extra.Temporals.previousWorkingDay () )
java.time
Java 8 and later have a built-in java.time environment. Inspired by Joda-Time , defined by JSR 310 and extended by ThreeTen-Extra .
These new classes replace the obviously annoying old time classes associated with the earliest versions of Java, java.util.Date/.Calendar. Avoid old classes where possible. When you need to interact, take a look at the recently added conversion methods to switch to java.time for most of your work. In addition, the creators of Joda-Time told us to switch to java.time as soon as it is convenient.
The basics of java.time ... Instant is the moment on the timeline in UTC. Apply the time zone ( ZoneId ) to get the ZonedDateTime . For a date value only without time or time zone, use LocalDate .
First we get “today” as an approximate date value. Note how the time zone is required to determine the current date, even if LocalDate does not contain a time zone. The date is not the same all over the world at the same time, since a new day comes earlier in the east.
LocalDate today = LocalDate.now ( ZoneId.of ( "America/Los_Angeles" ) );
regulators
The ThreeTen-Extra project extends java.time with additional or experimental features. These functions may or may not be bent in java.time. This project provides the Temporals class, which provides the implementation of controllers, including a pair for nextWorkingDay and previousWorkingDay . Ease of use as shown here.
At startup
Dump for the console. Please note that today is Friday, so the previous business day is -1 (yesterday, Thursday), and the next business day is +3 (Monday).
System.out.println ( "today: " + today + " | previousWorkingDay: " + previousWorkingDay + " | nextWorkingDay: " + nextWorkingDay );
today: 2016-01-22 | previousWorkingDay: 2016-01-21 | nextWorkingDay: 2016-01-25
Saturday and Sunday
This pair of knobs just skips every Saturday and Sunday. He does not know anything about the holidays. He is also unaware of other definitions of work week and weekend. The class documentation suggests writing your own java.time.temporal.TemporalAdjuster easily if you want to handle other definitions.