Getting the next valid day

I am writing one java application where I need to do the following.

I need to avoid two days a week (with the ability to configure from the outside), they say in TUESDAY and FRIDAY to do some processing of business logic. I want to find out the next day. For example: if TUESDAY is today, I have to get WEDNESDAY as the next available or if THURSDAY today, then the next available should be MONDAY.

Can someone help me solve this? It sounds simple, but it really is a trick.

Here is what I have done so far

List<String> exceptionDays = new ArrayList<String>(); exceptionDays.add("SUNDAY"); exceptionDays.add("MONDAY"); exceptionDays.add("FRIDAY"); Date today = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(today); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); if (dayOfWeek == Calendar.FRIDAY) { calendar.add(Calendar.DATE, 3); } else if (dayOfWeek == Calendar.SATURDAY) { calendar.add(Calendar.DATE, 2); } else { calendar.add(Calendar.DATE, 1); } String strDateFormat = "E"; SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat); strDateFormat = "EEEE"; sdf = new SimpleDateFormat(strDateFormat); Date nextBusinessDay = calendar.getTime(); System.out.println(sdf.format(nextBusinessDay).toUpperCase()); if(exceptionDays.contains(sdf.format(nextBusinessDay).toUpperCase())){ if(sdf.format(nextBusinessDay).toUpperCase().equals("FRIDAY")){ calendar.add(Calendar.DATE, 3); }else if(sdf.format(nextBusinessDay).toUpperCase().equals("SATURDAY")){ calendar.add(Calendar.DATE, 2); } else calendar.add(Calendar.DATE, 1); nextBusinessDay = calendar.getTime(); } DateFormat df = new SimpleDateFormat("dd-MMM-yy"); String format = df.format(nextBusinessDay.getTime()); System.out.println("Today : " + df.format(today)); try { System.out.println("Next business day: " + df.parse(format)); System.out.println("Next business day: " + df.format(nextBusinessDay)); 

Please ignore the logic above if it looks awkward.

0
source share
1 answer

Simple use of Calendar

 List<Integer> avoidingDays = new ArrayList<Integer>(); avoidingDays.add(Calendar.MONDAY); Calendar cal = Calendar.getInstance(); while(avoidingDays.contains(cal.get(Calendar.DAY_OF_WEEK)) ){cal.add(Calendar.DATE, 1} return cal; 
+1
source

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


All Articles