问题描述
我正在写一个java应用程序,我需要执行以下操作。我必须避免两个星期(外部可配置),说TUESDAY和FRIDAY到做一些业务逻辑处理。我想找出下一个可用的日子。例如:如果今天是TUESDAY,我应该得到WEDNESDAY作为下一个可用或如果今天是THURSDAY,那么下一个可以是MONDAY。
有人可以指导我解决这个问题?这听起来很简单,但它真的是三脚架。
这是我迄今为止所做的一切。
列表< String> exceptionDays = new ArrayList< String>();
exceptionDays.add(SUNDAY);
exceptionDays.add(MONDAY);
exceptionDays.add(FRIDAY);
日期date = new Date();
日历calendar = Calendar.getInstance();
calendar.setTime(今);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == Calendar.FRIDAY){
calendar.add(Calendar.DATE,3);
}其他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);
日期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(今天:+ df.format(今天));
try {
System.out.println(下一个工作日:+ df.parse(format));
System.out.println(下一个工作日:+ df.format(nextBusinessDay));
如果看起来很笨拙,请忽略上述逻辑。
简单使用日历
列表<整数> avoidDays = new ArrayList< Integer>();
/ pre>
avoidDays.add(Calendar.MONDAY);
日历cal = Calendar.getInstance();
while(avoidDays.contains(cal.get(Calendar.DAY_OF_WEEK))){cal.add(Calendar.DATE,1}
return cal;
I am writing one java application where i need to do the following.
I have to avoid two days of a week (externally configurable) say TUESDAY and FRIDAY to do some business logic processing. I want to find out the next available day. For Example : if today is TUESDAY, I should get WEDNESDAY as next available or if today is THURSDAY, then the next available should be MONDAY.
Could someone guide me solving this ? This sounds simple but its really trickey.
Here is what i did 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 above logic if it looks clumsy.
解决方案Simple use
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;
这篇关于获取下一个有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!