Pointing to the previous business day using java

I am new to using java.calendar.api. I want to point to the previous working day during the day using java. BUT conditions increase when I use calendar.api to manage dates since I had to look at regular weekends and pointing to the previous month, and I also had to look at regional holidays in my region ......

for ex: let's say I had to consider the US holidays and point to the day before.

Is there a way I can define my own calendar and use it so that date manipulations perceive all these usual changes?

+6
source share
4 answers

While you should use the Joda Time library, start with the Java Calendar API:

public Date getPreviousWorkingDay(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayOfWeek; do { cal.add(Calendar.DAY_OF_MONTH, -1); dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); } while (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY); return cal.getTime(); } 

This applies only to weekends. You will have to add additional checks to handle the days that you consider holidays. For example, you can add || isHoliday(cal) || isHoliday(cal) in the while condition. Then we implement this method, for example:

 public boolean isHoliday(Calendar cal) { int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); if (month == 12 && dayOfMonth == 25) { return true; } // more checks return false; } 
+8
source

Consider using Joda Time in conjunction with a list of regional holidays in your area.

+3
source

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.

 // The 'Temporals' class is from the ThreeTen-Extra library, not built into Java. LocalDate previousWorkingDay = today.with ( Temporals.previousWorkingDay () ); LocalDate nextWorkingDay = today.with ( Temporals.nextWorkingDay () ); 

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.

+2
source

You can define a class as shown below:

 import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * * @author j.james */ public class MyCalendar { private static Map<String, String> holidays = null; private static MyCalendar myCalendar = null; private static final int WEEKEND_1 = Calendar.SATURDAY; private static final int WEEKEND_2 = Calendar.SUNDAY; private MyCalendar() { holidays = new HashMap<String, String>(); holidays.put("7,4", "Independence Day"); holidays.put("12,25", "Christmas"); //holidays.putAll(DBUtils.readAnyDynamicHolidaysFromDB()); } public static Date getPreviousWorkingDay(Date date) { Date previousWorkingDate = null; try { if (myCalendar == null) { myCalendar = new MyCalendar(); } if(date != null) { Calendar calInstance = Calendar.getInstance(); calInstance.setTime(date); int weekDay = 0; do { calInstance.add(Calendar.DATE, -1); weekDay = calInstance.get(Calendar.DAY_OF_WEEK); } while(weekDay == WEEKEND_1 || weekDay == WEEKEND_2 || holidays.get((calInstance.get(Calendar.MONTH) + 1) + "," + calInstance.get(Calendar.DATE)) != null); previousWorkingDate = calInstance.getTime(); } } catch (Exception e) { e.printStackTrace(); } return previousWorkingDate; } } 

You can make a call as

 public static void main(String[] args) { System.out.println(MyCalendar.getPreviousWorkingDay(new Date(2011-1900,6,5))); //July 5, 2011 which returns July 1 as the working day because July 4th 2011 is Monday } 
0
source

Source: https://habr.com/ru/post/887428/


All Articles