我正在编写一个java应用程序,我需要执行以下操作。
我必须避免一周两天(外部可配置)说TUESDAY和FRIDAY做一些业务逻辑处理。我想找出下一个可用的日子。例如:如果今天是星期二,我应该在下一个可用的星期三或如果今天是星期四,那么下一个可用星期一。
有人可以指导我解决这个问题吗?这听起来很简单,但真的很棒。
这是我到目前为止所做的事情
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));
如果看起来很笨拙,请忽略上述逻辑。
答案 0 :(得分:1)
简单使用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;