Get the next business day after the specified date in JAVA

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class GetFirstWorkDate {

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// holiday List for test
		List<String> holidayList = new ArrayList<String>();
		holidayList.add("2019-10-13");
		holidayList.add("2019-10-14");
		holidayList.add("2019-10-15");
		// holidayList.add("2019-10-16");
		// holidayList.add("2019-10-17");
		holidayList.add("2019-10-21");

		// start date
		String date = "2019-10-07";
		// first work day
		Date scheduleActiveDate;
		try {
			scheduleActiveDate = getFirstWorkDate(holidayList, date);
			System.out.println("first work day:" + sdf.format(scheduleActiveDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public static Date getFirstWorkDate(List<String> list, String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// init start date
		Date currentDate = sdf.parse(date);
		Date nextDate;
		boolean delayFlg = false;

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("date", currentDate);
		map.put("delayFlg", delayFlg);

		// get the next work day
		HashMap<String, Object> nextMap = getNextWorkDay(list, map);
		// compare the next work day with start date,if they are the same
		// day,then stop loop and return
		while (!(((Date) map.get("date")).compareTo(((Date) nextMap.get("date"))) == 0)
				|| ((((Date) map.get("date")).compareTo(((Date) nextMap.get("date"))) == 0
						&& (boolean) nextMap.get("delayFlg")))) {
			// reset the start date
			nextDate = (Date) nextMap.get("date");
			currentDate = (Date) map.get("date");
			delayFlg = (boolean) nextMap.get("delayFlg");
			if (currentDate.compareTo(nextDate) == 0 && delayFlg) {
				nextDate = getTargetDaysLater(nextDate, 1);
				delayFlg = false;
			}
			map.put("date", nextDate);
			map.put("delayFlg", delayFlg);
			// get the next work day
			nextMap = getNextWorkDay(list, map);
		}
		return (Date) nextMap.get("date");
	}

	public static HashMap<String, Object> getNextWorkDay(List<String> list, HashMap<String, Object> map)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		HashMap<String, Object> mapTmp = new HashMap<String, Object>();
		Date date = (Date) map.get("date");
		boolean delayFlg = (boolean) map.get("delayFlg");
		Calendar calendar = getCalendar(date);
		if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			if (isHoliday(sdf.format(date), list)) {
				delayFlg = true;
			}
			date = getTargetDaysLater(date, 1);

		} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY
				|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY
				|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY
				|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
			if (isHoliday(sdf.format(date), list)) {
				date = getTargetDaysLater(date, 1);
			}
		} else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
			if (isHoliday(sdf.format(date), list)) {
				date = getTargetDaysLater(date, 1);
			}

		} else {
			date = getTargetDaysLater(date, 1);
		}
		// System.out.println("getNextDay:" + sdf.format(date));

		mapTmp.put("date", date);
		mapTmp.put("delayFlg", delayFlg);
		return mapTmp;
	}

	/**
	 * get the delayed date with given days
	 * 
	 * @param Date
	 *            start date
	 * @param int
	 *            delay days
	 * @return
	 */
	public static Date getTargetDaysLater(Date date, int n) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +n);
		date = calendar.getTime();
		return date;
	}

	/**
	 *
	 * @param date
	 * @return Calendar
	 * @throws ParseException
	 */
	public static Calendar getCalendar(Date date) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	/**
	 *
	 * @param sdate
	 * @param list
	 * @return
	 * @throws ParseException
	 */
	public static boolean isHoliday(String sdate, List<String> list) throws ParseException {
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (sdate.equals(list.get(i))) {
					return true;
				}
			}
		}
		return false;
	}
}

Recommended Posts

Get the next business day after the specified date in JAVA
How to get the date in java
[Java1.8 +] Get the date of the next x day of the week with LocalDate
Get the result of POST in Java
[Java] Get the day of the specific day of the week
[Java] Get the date with the LocalDateTime class
How to get the value after "_" in Windows batch like Java -version
Represents "next day" and "previous day" in Java / Android
Date manipulation in Java 8
Get the URL of the HTTP redirect destination in Java
Source used to get the redirect source URL in Java
[Java] Get the date 10 days later with the Calendar class
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
Get EXIF information in Java
Filter the Java No. 2 Optional list that was useful in business and get the first value
[Java] Get KClass in Java [Kotlin]
Count the number of digits after the decimal point in Java
How to derive the last day of the month in Java
Java is the 5th day
View current date in Java
[Java] Get the dates of the past Monday and Sunday in order
Get the current date and time by specifying the time zone in Thymeleaf
Get the public URL of a private Flickr file in Java
How to get the length of an audio file in java
Access the network interface in Java
Guess the character code in Java
Date processing in Java (LocalDate: Initialization)
[Android] Get the date on Monday
Specify the java location in eclipse.ini
Get Null-safe Map values in Java
Unzip the zip file in Java
Parsing the COTOHA API in Java
Get stuck in a Java primer
Call the super method in Java
[Java] How to get the current date and time and specify the display format
How to get the current date as a string in yyyyMMdd format
This and that of the implementation of date judgment within the period in Java
I want to get the IP address when connecting to Wi-Fi in Java
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
How to get date data in Ruby
Java reference to understand in the figure
Studying Java 8 (date API in java.time package)
Try using the Stream API in Java
Call the Windows Notification API in Java
Implement the algorithm in Ruby: Day 1 -Euclidean algorithm-
I tried the new era in Java
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
Try calling the CORBA service in Java 11+
[Java] How to set the Date time to 00:00:00
Output Date in Java in ISO 8601 extended format
[Java] How to get the current directory
[day: 5] I summarized the basics of Java
What is the main method in Java?
Deletes after the specified character from the character string
Get history from Zabbix server in Java
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)