根据请假开始时间和结束时间获得天数小时数分钟数
先来说下总体思路 :
根据开始日期结束日期获取,具体的每日日期,从而去掉放假日 根据日期获得是否为周末,同时加上不假日 , 获得最终请假日期List。
获取请假小时数分钟数:
同天是 end-begin
不是同一天,下班-begin+ end-上班
大体是这样。
一,main方法
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
String beginDay="2017-07-25"; //开始日期
Date begin=sdf.parse(beginDay);
String endDay="2017-07-26"; //结束日期
Date end=sdf.parse(endDay);
List<Date> dateList= this.getBetweenDates(begin, end);
int day= dateList.size();
this.getHourMinOfDate(null,null,day);
for(Date d:dateList){
this.getWeekOfDate(d);
}
} catch (Exception e) {
// TODO: handle exception
logger.error("结果(^|^)异常 e:"+e);
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
String beginDay="2017-07-25"; //开始日期
Date begin=sdf.parse(beginDay);
String endDay="2017-07-26"; //结束日期
Date end=sdf.parse(endDay);
List<Date> dateList= this.getBetweenDates(begin, end);
int day= dateList.size();
this.getHourMinOfDate(null,null,day);
for(Date d:dateList){
this.getWeekOfDate(d);
}
} catch (Exception e) {
// TODO: handle exception
logger.error("结果(^|^)异常 e:"+e);
}
二 , 获取请假日期
private List<Date> getBetweenDates(Date begin, Date end) {
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
end.getHours();
while(begin.getTime()<=end.getTime()){
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
Date timeDate=tempStart.getTime();
List<String> timeList=new ArrayList<String>();
//放假日
String sdofString="2017-01-02|2017-01-27|2017-01-30|2017-08-04|2017-02-01|2017-02-02|2017-04-03|2017-04-04|2017-05-01|2017-05-29|2017-05-30|2017-10-02|2017-10-03|2017-10-04|2017-10-05|2017-10-06|2017-07-02|2017-08-13";
String str=sdf1.format(timeDate);
//补假日
String buString="2017-01-22|2017-08-05|2017-08-01|2017-05-27|2017-09-30|2017-06-25|2017-09-24";
if(!sdofString.contains(str)){
String day= this.getWeekOfDate(timeDate);
String days="1|2|3|4|5";
if(days.contains(day)||buString.contains(str)){
result.add(timeDate);
System.out.println(str+"------------------------"+day);
}
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
end.getHours();
while(begin.getTime()<=end.getTime()){
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
Date timeDate=tempStart.getTime();
List<String> timeList=new ArrayList<String>();
//放假日
String sdofString="2017-01-02|2017-01-27|2017-01-30|2017-08-04|2017-02-01|2017-02-02|2017-04-03|2017-04-04|2017-05-01|2017-05-29|2017-05-30|2017-10-02|2017-10-03|2017-10-04|2017-10-05|2017-10-06|2017-07-02|2017-08-13";
String str=sdf1.format(timeDate);
//补假日
String buString="2017-01-22|2017-08-05|2017-08-01|2017-05-27|2017-09-30|2017-06-25|2017-09-24";
if(!sdofString.contains(str)){
String day= this.getWeekOfDate(timeDate);
String days="1|2|3|4|5";
if(days.contains(day)||buString.contains(str)){
result.add(timeDate);
System.out.println(str+"------------------------"+day);
}
}
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
三 判断 每日日期为周几。。。
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
/* Date timeDate=tempStart.getTime();
String str=sdf1.format(timeDate);
List<String> timeList=new ArrayList<String>();
String sdofString="2017-01-02|2017-01-27|2017-01-30|2017-01-31|2017-02-01|2017-02-02|2017-04-03|2017-04-04|2017-05-01|2017-05-29|2017-05-30|2017-10-02|2017-10-03|2017-10-04|2017-10-05|2017-10-06|2017-07-02|2017-08-13";
if(!str.contains(sdofString)){
String day= this.getWeekOfDate(timeDate);
String days="1|2|3|4|5";
if(days.contains(day)){
result.add(timeDate);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
System.out.println("------------------------"+day);
}
}*/
}
String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
/* Date timeDate=tempStart.getTime();
String str=sdf1.format(timeDate);
List<String> timeList=new ArrayList<String>();
String sdofString="2017-01-02|2017-01-27|2017-01-30|2017-01-31|2017-02-01|2017-02-02|2017-04-03|2017-04-04|2017-05-01|2017-05-29|2017-05-30|2017-10-02|2017-10-03|2017-10-04|2017-10-05|2017-10-06|2017-07-02|2017-08-13";
if(!str.contains(sdofString)){
String day= this.getWeekOfDate(timeDate);
String days="1|2|3|4|5";
if(days.contains(day)){
result.add(timeDate);
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
System.out.println("------------------------"+day);
}
}*/
}
四,获得 请假时分。
public static String getHourMinOfDate(String benginString ,String endString,int days){
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
benginString="09:30:00";//请假开始时间
endString="14:30:00";//请假结束时间
try {
String endWork="17:30:00";//下班时间
Date work1=df.parse(endWork);
String beginWork="9:00:00";//上班时间
Date work2=df.parse(beginWork);
long llll=work1.getTime()-work2.getTime();
Date begin = df.parse(benginString);
Date end = df.parse(endString);
long l = 0;
long hour=0;
long min=0;
if(days==1){
l= end.getTime() - begin.getTime();
if(llll-l>0){
days=0;
hour = (l / (60 * 60 * 1000) );
if(hour>=8){
hour=0;
}else {
min = ((l / (60 * 1000)) - hour * 60);
}
}else {
hour=0;
min=0;
}
}else {
long ll= work1.getTime() - begin.getTime();
long lll= end.getTime() - work2.getTime();
long det=(days * llll-(ll+lll));
if(det>0){
hour = ((ll+lll- (days-1) * llll) / (60 * 60 * 1000));
min = (((lll+ll- (days-1) * llll) / (60 * 1000)) - hour * 60);
}else {
hour = ((ll+lll- days * llll) / (60 * 60 * 1000));
min = (((lll+ll- days * llll) / (60 * 1000))- hour * 60);
}
days--;
}
System.out.println(days+"天"+hour+"小时"+min+"分钟");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
benginString="09:30:00";//请假开始时间
endString="14:30:00";//请假结束时间
try {
String endWork="17:30:00";//下班时间
Date work1=df.parse(endWork);
String beginWork="9:00:00";//上班时间
Date work2=df.parse(beginWork);
long llll=work1.getTime()-work2.getTime();
Date begin = df.parse(benginString);
Date end = df.parse(endString);
long l = 0;
long hour=0;
long min=0;
if(days==1){
l= end.getTime() - begin.getTime();
if(llll-l>0){
days=0;
hour = (l / (60 * 60 * 1000) );
if(hour>=8){
hour=0;
}else {
min = ((l / (60 * 1000)) - hour * 60);
}
}else {
hour=0;
min=0;
}
}else {
long ll= work1.getTime() - begin.getTime();
long lll= end.getTime() - work2.getTime();
long det=(days * llll-(ll+lll));
if(det>0){
hour = ((ll+lll- (days-1) * llll) / (60 * 60 * 1000));
min = (((lll+ll- (days-1) * llll) / (60 * 1000)) - hour * 60);
}else {
hour = ((ll+lll- days * llll) / (60 * 60 * 1000));
min = (((lll+ll- days * llll) / (60 * 1000))- hour * 60);
}
days--;
}
System.out.println(days+"天"+hour+"小时"+min+"分钟");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}