网站建设资讯

NEWS

网站建设资讯

java放假代码,JaVa代码

使用java 排除 两个日期段中的 休息日和节假日 。在线等待中十万火急,求大牛帮忙,多谢了。。。。

时间处理,更方便和更易读的代码角度讲,肯定要用Java8的新date api啦,由于涉及到一系列日期,所以结合Java8的Stream也是理所当然

成都创新互联公司专注于企业营销型网站建设、网站重做改版、通道网站定制设计、自适应品牌网站建设、HTML5建站成都商城网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为通道等各大城市提供网站开发制作服务。

总体思路:

由于节假日每年在变,所以需要罗列出所有的节假日集合A

生成2016-01-01 至 2016-05-01所有的日期,生成日期集合B

从B中过滤掉A中的节假日

从B中过滤掉周六周日

最后把B中集合打印

结合思路,所见即所得的代码如下:

// 所有节假日的日期集合,这里你可以自己添加,只写了两个仅供参考(完成思路1)

ListLocalDate holidays = Arrays.asList(LocalDate.parse("2016-01-01"), LocalDate.parse("2016-05-01"));

// 按照起始2016-01-01,每次递增一天的方式生成一个Stream

Stream.iterate(LocalDate.parse("2016-01-01"), localDate - localDate.plusDays(1))

// 按照要求的时间间隔2016-01-01 至 2016-05-01中的实际间隔天数截断Stream(完成思路2)

.limit(ChronoUnit.DAYS.between(LocalDate.parse("2016-01-01"), LocalDate.parse("2016-05-01")))

// 过滤其中的节假日(完成思路3)

.filter(localDate - !holidays.contains(localDate))

// 过滤其中的周六

.filter(localDate - !DayOfWeek.SATURDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))))

// 过滤其中的周日(完成思路4)

.filter(localDate - !DayOfWeek.SUNDAY.equals(DayOfWeek.of(localDate.get(ChronoField.DAY_OF_WEEK))))

// 打印最后结果(完成思路5)

.forEach(System.out::println);

打印的结果:

综上:结合新时间API的易用性+Stream处理集合的快捷性,写出代码还是很简洁的

用java实现在控制台输出一年中所有节假日!

因为很多的节假日,没有规律性。所以程序实现只有写死日期。

可以做一个配置文件,手工填写假日信息,然后再由java显示出来。

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

迟来的答案

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

注意:最下面是使用的 递归算法

/**

 * 获得收益时间(获取当前天+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

一段可以跳过周末和节假日的java程序怎么写

public static boolean isWeekend(Date date) {

Calendar cal = Calendar.getInstance();

cal.setTime(date);

int result = cal.get(Calendar.WEEK_OF_MONTH);

if (result == Calendar.SATURDAY || result == Calendar.SUNDAY) {

return true;

}

return false;

}

这段代码只是判断是不是周末,节假日的话只能通过某些数据配置来判断了。节假日是没法计算的。


网页标题:java放假代码,JaVa代码
路径分享:http://cdweb.net/article/heicsh.html