java节假日算法_用java 进行日期计算,获取当前天+1天,周末节假日不算,在线等待中,十万火急……...

展开全部

迟来的答案

1.周末版本(不含节假日判断)

注意:最下面是使用的 递归32313133353236313431303231363533e58685e5aeb931333335326262算法/**

* 获得收益时间(获取当前天+1天,周末不算).

*

* @param date

*            任意日期

* @return the income date

* @throws NullPointerException

*             if null == date

*/

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的操作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

//+1天

calendar.add(Calendar.DAY_OF_MONTH, +1);

//判断是星期几

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

Date incomeDate = calendar.getTime();

if (dayOfWeek == 1 || dayOfWeek == 7){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

测试方法:

@Test

public void testGetIncomeDate() throws ParseException{

String pattern = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

System.out.println(simpleDateFormat.format(getIncomeDate(new Date())));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-07-31 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-01 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-02 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-03 13:33:05"))));

}

输出结果:

2014-08-01 13:48:09

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

注意:返回的是 时间+1的时间,精度是到毫秒 纳秒,如果有特殊需求,需要自己再处理下

2.周末+节假日版本/**

* 获得收益时间(获取当前天+1天,周末不算).

*

* @param date

*            任意日期

* @return the income date

* @throws NullPointerException

*             if null == date

*/

private Date getIncomeDate(Date date) throws NullPointerException{

if (null == date){

throw new NullPointerException("the date is null or empty!");

}

//对日期的操作,我们需要使用 Calendar 对象

Calendar calendar = new GregorianCalendar();

calendar.setTime(date);

//+1天

calendar.add(Calendar.DAY_OF_MONTH, +1);

Date incomeDate = calendar.getTime();

if (isWeekend(calendar) || isHoliday(calendar)){

//递归

return getIncomeDate(incomeDate);

}

return incomeDate;

}

/**

* 判断一个日历是不是周末.

*

* @param calendar

*            the calendar

* @return true, if checks if is weekend

*/

private boolean isWeekend(Calendar calendar){

//判断是星期几

int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

if (dayOfWeek == 1 || dayOfWeek == 7){

return true;

}

return false;

}

/**

* 一个日历是不是节假日.

*

* @param calendar

*            the calendar

* @return true, if checks if is holiday

*/

private boolean isHoliday(Calendar calendar){

String pattern = "yyyy-MM-dd";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String dateString = simpleDateFormat.format(calendar.getTime());

//节假日 这个可能不同地区,不同年份 都有可能不一样,所以需要有个地方配置, 可以放数据库, 配置文件,环境变量 等等地方

//这里以配置文件 为例子

ResourceBundle resourceBundle = ResourceBundle.getBundle("holidayConfig");

String holidays = resourceBundle.getString("holiday");

String[] holidayArray = holidays.split(",");

boolean isHoliday = org.apache.commons.lang.ArrayUtils.contains(holidayArray, dateString);

return isHoliday;

}

配置文件:

holiday=2014-10-01,2014-10-02,2014-10-03,2014-10-04,2014-10-05,2014-10-06,2014-10-07

测试方法 :/**

* Testenclosing_type.

*

* @throws ParseException

*             the parse exception

*/

@Test

public void testGetIncomeDate() throws ParseException{

String pattern = "yyyy-MM-dd HH:mm:ss";

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

System.out.println(simpleDateFormat.format(getIncomeDate(new Date())));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-07-31 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-01 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-02 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-08-03 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-09-30 13:33:05"))));

System.out.println(simpleDateFormat.format(getIncomeDate(simpleDateFormat.parse("2014-10-02 13:33:05"))));

}

结果:

2014-08-01 15:14:59

2014-08-01 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-08-04 13:33:05

2014-10-08 13:33:05

2014-10-08 13:33:05

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值