问题描述
假设我在数据库中存储了一堆假期.
Let's say I have stored a bunch of holidays in my database.
我需要做的是找出下一个工作日,不包括数据库中定义的星期六和公众假期.
What I need to do is find out which is next work day excluding Saturdays and the public holidays defined in the database.
例如
比方说,今天是2月15日(星期五),而17日和18日是公众假期(在数据库中定义为日期时间).因此,现在当我按下显示下一个工作日的按钮时,它将返回2月19日.
Let's say today is the Friday the 15th Feb and the 17th and the 18th are public holidays as defined in the database as datetime. So now when I press a button that says next work day, it should return 19th Feb.
哪种方法最有效?
推荐答案
最简单.
第1步:从数据库获取假期并将其格式化为您的格式,并将其保存在 List< String>
Step 1: Get holidays from DB and format to your format, keep it in a List<String>
第2步:创建一个添加日的方法.
Step 2: Create a method that adds day.
public static Date addDays(Date d, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(d);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
第3步:创建一种寻找假期的方法.
Step 3: Create a method to find holiday.
public boolean isBankHoliday(java.util.Date d) {
Calendar c = new GregorianCalendar();
c.setTime(d);
if((Calendar.SATURDAY == c.get(c.DAY_OF_WEEK)) || (Calendar.SUNDAY == c.get(c.DAY_OF_WEEK)) || bankHolidays.contains(dString)) {
return (true);
} else {
return false;
}
}
第4步:获取输入日期.检查银行假期,循环直到找到工作日.
Step 4: Get your input date. Check with bank holiday, loop until you find a working day.
while (isBankHoliday(myDate)) {
myDate = addDays(myDate, 1);
}
这篇关于寻找下一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!