package com.tjsoft.dzjc.supervise.bl.xzxk.busistat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateCale {
/**
主要思路:
对于任意2个日期比如:date_start=2006-10-1、date_end=2006-10-14 ,
首先计算这连个日期之间的时间间隔(天数),然后分别对date_start 和date_end 取得它们下一个星期一的日期,
这样就可以得到一个新的可以整除7的完整日期间隔(这个新的日期间隔已经把星期几的问题剔出掉了),
换一种说法就是我们可以得到,这两个新的日期之间的周数,拿这个周数乘以5就是工作日期了(tmpWorkingDays)。
但是这个日期并不是我们所要的日期,接下来我们要做的就是计算date_start,date_end这两个日期对于根据它们所产生的新的日期之间的时间偏移量,
date_start的偏移量(date_start_change)是需要加的,而date_end的这个偏移量(date_end_change)是需要减去的。
最后我们只要用tmpWorkingDays+date_start_change-date_end_change就是我们所要求的实际工作日了。
以下是所有实现代码(两个日期跨年也没有问题)。
*/
public static void main(String[] args) {
try {
// boolean b =doAddWeekDays("2010-02");
int b = new DateCale().getMonths("2010-02-01", "2010-03-05");
System.out.println(b);
} catch (Exception e) {
e.printStackTrace();
}
}
public int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
- d1.get(java.util.Calendar.DAY_OF_YEAR);
int y2 = d2.get(java.util.Calendar.YEAR);
if (d1.get(java.util.Calendar.YEAR) != y2) {
d1 = (java.util.Calendar) d1.clone();
do {
days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
d1.add(java.util.Calendar.YEAR, 1);
} while (d1.get(java.util.Calendar.YEAR) != y2);
}
return days;
}
/**
* 计算2个日期之间的相隔天数
* @param d1
* @param d2
* @return
*/
public int getWorkingDay(java.util.Calendar d1, java.util.Calendar d2) {
int result = -1;
if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
java.util.Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int charge_start_date = 0;//开始日期的日期偏移量
int charge_end_date = 0;//结束日期的日期偏移量
// 日期不在同一个日期内
int stmp;
int etmp;
stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
if (stmp != 0 && stmp != 6) {// 开始日期为星期六和星期日时偏移量为0
charge_start_date = stmp - 1;
}
if (etmp != 0 && etmp != 6) {// 结束日期为星期六和星期日时偏移量为0
charge_end_date = etmp - 1;
}
// }
result = (getDaysBetween(this.getNextMonday(d1), this.getNextMonday(d2)) / 7)
* 5 + charge_start_date - charge_end_date;
return result;
}
public String getChineseWeek(Calendar date) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);
// System.out.println(dayNames[dayOfWeek - 1]);
return dayNames[dayOfWeek - 1];
}
/**
* 获得日期的下一个星期一的日期
*
* @param date
* @return
*/
public Calendar getNextMonday(Calendar date) {
Calendar result = null;
result = date;
do {
result = (Calendar) result.clone();
result.add(Calendar.DATE, 1);
} while (result.get(Calendar.DAY_OF_WEEK) != 2);
return result;
}
/**
*
* @param d1
* @param d2
* @return
*/
public int getHolidays(Calendar d1,Calendar d2){
return this.getDaysBetween(d1, d2)-this.getWorkingDay(d1, d2);
}
public static boolean doReplace(String sbtime){
boolean flag = true;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String xgtime = sdf.format(new Date()); //上报时间
System.out.println("newdate :"+xgtime);
String limitedTime = RWProp.getProperty("db.properties", "time");
int limitTime = Integer.parseInt(limitedTime);
System.out.println("limitedTime: " + limitedTime);
//获取上报时间的星期
int day = BusiStatDeal.getWeekday(sbtime);
//计算上报时间与修改当天之间间隔多少个工作日)
try {
Date date_start = sdf.parse(sbtime);
Date date_end = sdf.parse(xgtime);
DateCale app = new DateCale();
Calendar cal_start = Calendar.getInstance();
Calendar cal_end = Calendar.getInstance();
cal_start.setTime(date_start);
cal_end.setTime(date_end);
System.out.println("星期-->" + app.getChineseWeek(cal_start)
+ " 日期-->" + cal_start.get(Calendar.YEAR) + "-"
+ (cal_start.get(Calendar.MONTH) + 1) + "-"
+ cal_start.get(Calendar.DAY_OF_MONTH));
System.out.println("星期-->" + app.getChineseWeek(cal_end) + " 日期-->"
+ cal_end.get(Calendar.YEAR) + "-"
+ (cal_end.get(Calendar.MONTH) + 1) + "-"
+ cal_end.get(Calendar.DAY_OF_MONTH));
int weekDays = app.getWorkingDay(cal_start, cal_end);
if(day<6){//如果上报时间为工作日的时间,则当天也计算在内
weekDays+=1;
}
System.out.println("工作日为-->" +weekDays);
if(limitTime<weekDays){//限制时间小于间隔的工作日,则无修改权利,否则就可以修改上报说明
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static boolean doQueryWeekDays(String sbtime){
boolean flag = true;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String xgtime = sdf.format(new Date()); //修改时间
System.out.println("newdate :"+xgtime);
String limitedTime = BusiStatDeal.doQueryTime();
int limitTime = Integer.parseInt(limitedTime);
System.out.println("limitedTime: " + limitedTime);
//如果是从上报时间的下月开始时间开始的话。。。。
//获取上报时间下月首日的星期
String nextFDay = BusiStatDeal.getNextFDay(sbtime);
int day = BusiStatDeal.getWeekday(nextFDay);
//计算上报时间与修改当天之间间隔多少个工作日)
try {
Date date_start = sdf.parse(nextFDay);
Date date_end = sdf.parse(xgtime);
DateCale app = new DateCale();
Calendar cal_start = Calendar.getInstance();
Calendar cal_end = Calendar.getInstance();
cal_start.setTime(date_start);
cal_end.setTime(date_end);
int weekDays = app.getWorkingDay(cal_start, cal_end);
if(day<6){//如果上报时间为工作日的时间,则当天也计算在内
weekDays+=1;
}
System.out.println("工作日为-->" +weekDays);
if(limitTime<weekDays){//限制时间小于间隔的工作日,则无修改权利,否则就可以修改上报说明
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 计算当前时间距本月的首日的工作日是否超出了系统期限工作日
* @return
*/
public static boolean doAddWeekDays(String times){
boolean flag = true;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String xgtime = sdf.format(new Date()); //添加时间
System.out.println("newdate :"+xgtime);
String limitedTime = BusiStatDeal.doQueryTime();
int limitTime = Integer.parseInt(limitedTime);
System.out.println("limitedTime: " + limitTime);
//获取添加时间的当月首日的日期
String currentFDay = BusiStatDeal.getNextFDay(times);
//获取添加时间的当月首日的星期
int day = BusiStatDeal.getWeekday(currentFDay);
//计算本月首日与添加当天之间间隔多少个工作日)
try {
Date date_start = sdf.parse(currentFDay);
Date date_end = sdf.parse(xgtime);
DateCale app = new DateCale();
Calendar cal_start = Calendar.getInstance();
Calendar cal_end = Calendar.getInstance();
cal_start.setTime(date_start);
cal_end.setTime(date_end);
int weekDays = app.getWorkingDay(cal_start, cal_end);
if(day<6){//如果上报时间为工作日的时间,则当天也计算在内
weekDays+=1;
}
System.out.println("工作日为-->" +weekDays);
if(limitTime<weekDays){//限制时间小于间隔的工作日,则无修改权利,否则就可以修改上报说明
flag = false;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static String getDayOfLSH(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String date = sdf.format(new Date());
return date;
}
public static String getDays(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sdf.format(new Date());
return date;
}
public static boolean getDayInMonth(String sbtime){//查看修改当天是否在本月内,如果在本月内修改,则不需要计算时限的工作日
boolean flag = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
String new_month = date.split("-")[1];
String old_month = sbtime.split("-")[1];
int n = Integer.parseInt(new_month);
int o = Integer.parseInt(old_month);
if(n==o && n!=0 && o!=0){
flag = true;
}
return flag;
}
/**
* 计算指定日期之间有几个月份
* @param date11 指定时间1
* @param date22 指定时间2
* @return
*/
public static int getMonths(String date11, String date22){
int iMonth = 0;
int flag = -1;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(date11);
Calendar objCalendarDate1 = Calendar.getInstance();
objCalendarDate1.setTime(date1);
Date date2 = sdf.parse(date22);
Calendar objCalendarDate2 = Calendar.getInstance();
objCalendarDate2.setTime(date2);
if (objCalendarDate2.equals(objCalendarDate1))
return 0;
if (objCalendarDate1.after(objCalendarDate2)){
Calendar temp = objCalendarDate1;
objCalendarDate1 = objCalendarDate2;
objCalendarDate2 = temp;
}
if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1.get(Calendar.DAY_OF_MONTH))
flag = 1;
if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1.get(Calendar.YEAR))
iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1.get(Calendar.YEAR))
* 12 + objCalendarDate2.get(Calendar.MONTH) - flag)
- objCalendarDate1.get(Calendar.MONTH);
else
iMonth = objCalendarDate2.get(Calendar.MONTH)
- objCalendarDate1.get(Calendar.MONTH) - flag;
} catch (Exception e){
e.printStackTrace();
}
return iMonth;
}
}