1、假期实体类:

  

package com.smics.date;

import java.io.Serializable;
import java.util.Date; public class Vacation implements Serializable { private static final long serialVersionUID = 1L;
private Date date;
private int days; public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} public int getDays() {
return days;
} public void setDays(int days) {
this.days = days;
} }

2、日期之间的假期数目

  

package com.smics.date;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner; @SuppressWarnings({"rawtypes","unchecked"})
public class DateToDate { /***************************************************************************
* 在beginDate和endDate之间所包含假期的天数,若不包含则返回0; 假期和周末分别进行单独考虑!
*
* @param list
* @param beginDate
* @param endDate
* @return
*/
public static int getContainVacation(List list, Date beginDate, Date endDate) {
int days = 0;
Calendar begincal = new GregorianCalendar();
Calendar endcal = new GregorianCalendar();
begincal.setTime(beginDate);
endcal.setTime(endDate); for (int i = 0; i < list.size(); i++) {
System.out.println("执行到这里了!");
Vacation vac = (Vacation) list.get(i);
Date tempDate = vac.getDate();
Calendar tempcal = new GregorianCalendar();
tempcal.setTime(tempDate);
int tempDays = vac.getDays();
int tempDay = tempcal.get(Calendar.DAY_OF_YEAR);
int dd = 0;
if ((tempDate.after(endDate)) || (tempDate.before(beginDate))) {
System.out.println(tempDate.after(endDate));
System.out.println("执行到这里了吗???!");
continue;
} else {
System.out.println("应该执行到这里了!@");
while (tempDay < endcal.get(Calendar.DAY_OF_YEAR)
&& dd < tempDays) {
System.out.println("符合条件吗????");
// tempcal.set(Calendar.DAY_OF_MONTH,tempDay);//原来是你在作怪!
// 节假日和周末可能有重叠的情况!
if ((tempcal.get(Calendar.DAY_OF_WEEK)) == 2
|| (tempcal.get(Calendar.DAY_OF_WEEK) == 1)) {
System.out.println((tempcal.get(Calendar.DAY_OF_WEEK)) == 2);
System.out.println((tempcal.get(Calendar.DAY_OF_WEEK) == 1));
System.out.println("节假日和周末重叠的情况!"
+ tempcal.get(Calendar.DAY_OF_WEEK));
days--;
}
tempcal.add(Calendar.DAY_OF_WEEK, 1);
dd++;// 计数器自增,不能超出法定的假期数。
days++;// 符合这两种条件的自增。看一下有多少天!
tempDay++;// 法定假日自增,不能超出endDate的日期数!
}
}
}
// 单独考虑周末的情况!不知道哪一个数字代表周六,周日!
System.out.println("周末!");
for (int j = begincal.get(Calendar.DAY_OF_YEAR); j <= endcal
.get(Calendar.DAY_OF_YEAR); j++) {
if (begincal.get(Calendar.DAY_OF_WEEK) == 7
|| begincal.get(Calendar.DAY_OF_WEEK) == 1) {
System.out.println("周末判断!");
days++;
}
begincal.add(Calendar.DAY_OF_YEAR, 1);
}
return days;
} /***************************************************************************
* 从文件中读取字符串到集合中,然后返回集合。
*
* @param file
* @return
* @throws Exception
*/
public static List getDateFromFile(File file) throws Exception {
List list = new ArrayList();
BufferedReader breader = new BufferedReader(new FileReader(file));
String str = "";
while ((str = breader.readLine()) != null) {
Vacation vac = DateToDate.divideStr(str);
list.add(vac);
}
System.out.println(list);
return list;
} /***************************************************************************
* 将字符串最终切割成一个假期对象!
*
* @param str
* @return
* @throws ParseException
*/
public static Vacation divideStr(String str) throws ParseException {
Vacation vac = new Vacation();
String[] array = str.split(",");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(array[0]);
int days = Integer.parseInt(array[1]);
vac.setDate(date);
vac.setDays(days);
return vac;
} /***************************************************************************
* 输入的前后两天之间的相差的天数!
*
* @param beginDate
* @param endDate
* @return
*/
public static int getDays(Date beginDate, Date endDate) {
long days = 0;
try {
if (beginDate.compareTo(endDate) > 0) {
throw new IllegalArgumentException("日期输入不正确!");
}
days = (endDate.getTime() - beginDate.getTime())
/ (1000 * 60 * 60 * 24);
} catch (Exception e) {
e.getStackTrace();
}
return (int) days;
} /***
* 从键盘标准输入两个日期!
*
* @return
*/
public static String[] getInDate() {
System.out.println("请输入开始和结束日期!格式如下:yyyy-MM-dd");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] array = str.split(",");
return array;
} public static void main(String args[]) {
String[] str = DateToDate.getInDate();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date beginDate = format.parse(str[0]);
Date endDate = format.parse(str[1]);
File file = new File("D:\\java\\file\\jinxing.txt");
List list = DateToDate.getDateFromFile(file);
int days = DateToDate.getContainVacation(list, beginDate, endDate);
System.out.println("总共的节假日包括周末:" + days);
int allday = DateToDate.getDays(beginDate, endDate);
System.out.println("总共的天数:" + allday);
System.out.println("总共的工作日为:" + (allday - days));
} catch (Exception e) {
e.getStackTrace();
}
} }

3、计算工作日之后N天的日期

  

package com.smics.date;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner; /**
* Gets the date of N days after the specified date
* @package com.smics.date
* @file WorkDate.java
* @author E047352
* @date 2018-12-11 PM 05:11:50
* @version V 1.0
*/
public class WorkDate { /***************************************************************************
* Calculate the date after the working day by the given date and working day!
*
* @param beginDate
* @param workDays
*/
public static Date getDate(Date beginDate, int workDays, List<Vacation> list) {
Date endDate = beginDate;
Calendar calBegin = Calendar.getInstance();
calBegin.setTime(beginDate);
int count = 1;
Calendar calFile = Calendar.getInstance();
while (count <= workDays) {
int tempBeginWeek = calBegin.get(Calendar.DAY_OF_WEEK);
if (tempBeginWeek < 7 && tempBeginWeek > 1) {
// Number of days to cycle its holidays!
for (int i = 0; i < list.size(); i++) {
//System.out.println("It should be recycled at least twice!!");
Vacation vac = (Vacation) list.get(i);
Date fileDate = vac.getDate();
calFile.setTime(fileDate);
int fileDay = vac.getDays();
int tempFileDays = calFile.get(Calendar.DAY_OF_YEAR);// The Days of the Year
//System.out.println("Which day of the year is it:" + tempFileDays);
//System.out.println("What day is today:" + tempBeginWeek);
for (int j = tempFileDays; j < (tempFileDays + fileDay); j++) {
if (calBegin.get(Calendar.DAY_OF_YEAR) == j) { count--;
}
}
}
count++;
}
calBegin.add(Calendar.DATE, 1);
}
endDate = calBegin.getTime();
return endDate;
} /***************************************************************************
* Get holidays from files and return to a collection!
*
* @param file
* @return
* @throws Exception
*/
public static List<Vacation> getListVacation(File file) throws Exception {
List<Vacation> list = new ArrayList<Vacation>();
list = (List<Vacation>) WorkDate.getVacationDateFromFile(file);
return list;
} /***************************************************************************
* Standard input, input start date and workday parameters from the screen!!
*
* @return
*/
public static String[] getIn() {
System.out.println("Please enter the start date and workday parameter!!");
String str = "";
Scanner scanner = new Scanner(System.in);
str = scanner.nextLine();
String[] array = str.split(",");
while (true) {
if (array.length <= 1) {
System.out.println("Please enter the required parameters correctly!");
str = scanner.nextLine();
array = str.split(",");
} else {
break;
}
}
return array;
} /***************************************************************************
* Split the string and return a Vacation object!
*
* @param str
* @return
* @throws ParseException
*/
public static Vacation divideStr(String[] array) throws ParseException {
Vacation vac = new Vacation();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(array[0]);
vac.setDate(date);
int days = Integer.parseInt(array[1]);
vac.setDays(days);
return vac;
}
/**
* Read the string from the file into the collection, and then return to the collection.
*
* @param file
* @return
* @throws Exception
*/ public static List<Vacation> getVacationDateFromFile(File file)
throws Exception {
List<Vacation> list = new ArrayList<Vacation>();
BufferedReader breader = new BufferedReader(new FileReader(file));
String str = "";
while ((str = breader.readLine()) != null) {
Vacation vac = DateToDate.divideStr(str);
list.add(vac);
}
return list;
} public static void main(String args[]) {
try {
File file = new File("D:\\java\\file\\jinxing.txt");
List<Vacation> list = WorkDate.getListVacation(file);
Vacation vac = WorkDate.divideStr(WorkDate.getIn());
Date date = WorkDate.getDate(vac.getDate(), vac.getDays(), list);
System.out.println("Is this here?");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = format.format(date);
System.out.println("Dates after "+vac.getDays()+" working days are:" + strDate);
} catch (Exception e) {
e.printStackTrace();
}
} }

计算工作日之后N天的日期的更多相关文章

  1. 160_技巧_Power BI 新函数-计算工作日天数

    160_技巧_Power BI 新函数-计算工作日天数 一.背景 Power BI 2022 年 7 月 14 日更新了最新版本的,版本号为:2.107.683.0 . 更多更新内容可以查看官方博客: ...

  2. 【原创】java 获取十个工作日之前或之后的日期(算当天)-完美解决-费元星

    [原创]java 获取十个工作日之后的日期(算当天)-完美解决-费元星(仅考虑星期六星期天) /** * * 根据开始日期 ,需要的工作日天数 ,计算工作截止日期,并返回截止日期 * @param s ...

  3. 【原创】如何使用一句SQL计算工作日天数?

    现在有这样一个需求,要求计算两个日期间的工作日天数,要求除去节假日,其中节假日有一张配置表,具体的格式如下: 开始日期 结束日期 节假日类型 节假日名称 2013-08-10 2013-08-12   ...

  4. Java计算工作日的工具类

    有时候需要根据工作日计算指定的日期,也就是需要排除周六日. 1.  初版代码如下: package cn.xm.exam.utils; import java.util.Calendar; impor ...

  5. js 计算快速统计中用到的日期

    前言 最近在做统计报表模块,其中查询条件用到了快速查询,主要为了方便客户统计查询常用的几个日期纬度,比如本周.上周.本月.上月.昨日. 使用js计算,主要用到了js Date. getDate().g ...

  6. 计算当前日期n天后的日期

    //计算180天后的日期//180*24*60*60*1000//更具时间戳计算n天前的日期 $(function () { var timestamp =Date.parse(new Date()) ...

  7. java 根据某个数字,计算前后多少天的具体日期

    import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Calendar; import ...

  8. JS计算本周一和本周五的日期

    代码不长: var today=new Date();var weekday=today.getDay();    var monday=new Date(1000*60*60*24*(1-weekd ...

  9. C# 根据年月获得此月第一天和最后一天,并计算工作日

    string str = "2015年3月"; ); ); , secondIndex - firstIndex - ); , ); DateTime dt = DateTime. ...

随机推荐

  1. Git教程(3)git工作区与文件状态及简单示例

    基础 目录: working driectory  工作目录,就是我们的工作目录,其中包括未跟踪文件及暂存区和仓库目录. staging area   暂存区,不对应一个具体目录,其实只是git di ...

  2. Run as ant build每次都执行两次-问题解决

    在Eclipse里面,运行ant,整个测试流程总是执行两遍,其几天试了下在DOS命令行直接调用ant, 结果发现只执行了一次,并且内存消耗好像也没那么大了,估计是eclipse自己的问题.问题解决了, ...

  3. Elasticsearch之批量操作bulk

    1.bulk相当于数据库里的bash操作. 2.引入批量操作bulk,提高工作效率,你想啊,一批一批添加与一条一条添加,谁快? 3.bulk API可以帮助我们同时执行多个请求 4.bulk的格式: ...

  4. android黑科技系列——修改锁屏密码和恶意锁机样本原理分析

    一.Android中加密算法 上一篇文章已经介绍了Android中系统锁屏密码算法原理,这里在来总结说一下: 第一种:输入密码算法 将输入的明文密码+设备的salt值,然后操作MD5和SHA1之后在转 ...

  5. three.js 流程图

    用Axure做了个模型图:          第一步: Scene --模型.灯光.特效 第二步: Camera --视角 第三步: Renderer -- 渲染输出 第四步: render --渲染 ...

  6. Linux scp 后台运行传输文件

    Linux scp 设置nohup后台运行 1.正常执行scp命令 2.输入ctrl + z 暂停任务 3.bg将其放入后台 4.disown -h 将这个作业忽略HUP信号 5.测试会话中断,任务继 ...

  7. dubbo之只订阅及只注册

    只订阅 问题 如果有两个镜像环境,两个注册中心,有一个服务只在其中一个注册中心有部署,另一个注册中心还没来得及部署,而两个注册中心的其它应用都需要依赖此服务,所以需要将服务同时注册到两个注册中心,但却 ...

  8. [CefSharp] 如何在JavaScript中调用C#代码

    本例在WinForms下实现,具体流程与WPF一致. 本例仅供调用示例,不代表正常业务书写流程. 1. 创建WinForms项目,并将项目属性设置为x86平台 此处预先设置,避免引用时报错,再花更多的 ...

  9. C#异步Async、Task、Await

    参考http://www.cnblogs.com/jesse2013/p/async-and-await.html 事例: static void Main(string[] args) { ; i ...

  10. C# 截取字符串基本

    #region --构建字符串处理 string str1 = "123AAA456AAAA789AAAAAAA1011"; string str2 = "1234567 ...