package com.ds.task.controller;java
import org.apache.commons.lang3.StringUtils;apache
import java.math.BigDecimal;spa
import java.text.DateFormat;.net
import java.text.ParseException;orm
import java.text.SimpleDateFormat;ci
import java.util.Calendar;get
import java.util.Date;it
/**io
* Created by lenovo on 2016/1/6.form
*/
public class TestDelayHours {
public static void main(String[] args) {
//計算延遲工時
//format的格式要與日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat sdfs = new SimpleDateFormat("HH:mm:ss");
DateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
String planedFinishDate = "2016-1-1";
String nowTime = "2016-1-7 02:40:30";
//String planedFinishDate = itDevelopmentTask.getPlanedFinishDate();
//String nowTime = sdf.format(new Date());
//拆分出日期和時分秒
String[] completionTimeArr = nowTime.split(" ");
Date planedDate = null;
Date finishedDate = null;
long totalHours = 0;
if (StringUtils.isNotEmpty(nowTime) && StringUtils.isNotEmpty(planedFinishDate)) {
try {
planedDate = sdf.parse(planedFinishDate + " 18:30:00");
finishedDate = sdf.parse(nowTime);
//若是結束日期大於預計完成日期
if (finishedDate.after(planedDate)) {
long delay = finishedDate.getTime() - planedDate.getTime();
long timeDiff = delay / 1000 / 60 / 60 / 24;
for (int i = 1; i <= timeDiff; i++) {
Date date = getDateAfter(planedDate, i);
int weekday = getDayOfWeek(date);
if (weekday > 1 && weekday < 7) {
totalHours += 8;
}
}
double todayHours = 0;
Date completionTimeHour = sdfs.parse(completionTimeArr[1]);
Date moTime = sdfs.parse("9:00:00");
Date amTime = sdfs.parse("12:30:00");
Date afTime = sdfs.parse("14:00:00");
Date pmTime = sdfs.parse("18:30:00");
Date date = getDateAfter(finishedDate, 0);
int weekday = getDayOfWeek(date);
if (weekday > 1 && weekday < 7) {
//若是結束時間在9:00-12:30之間,計算時間差
if (completionTimeHour.after(moTime) && completionTimeHour.before(amTime)) {
todayHours =new BigDecimal((completionTimeHour.getTime() - moTime.getTime())/1000.0/60.0/60.0)
.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
//若是結束時間在14:00-18:30之間,計算時間差
} else if (completionTimeHour.after(afTime) && completionTimeHour.before(pmTime)) {
todayHours = new BigDecimal((completionTimeHour.getTime() - afTime.getTime())/1000.0/60.0/60.0 + 3.5)
.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
//若是結束時間在12:30-14:00之間,計算時間差
}else if (completionTimeHour.after(amTime) && completionTimeHour.before(afTime)){
todayHours = 3.5;
}
}
//itDevelopmentTask.setDelayHours((double) (totalHours + todayHours));
System.out.println(totalHours + todayHours);
}
}catch(ParseException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 獲取指定天數後的日期
* @param baseDate 基準日期
* @param day 後推天數
* @return 後推後的天數
*/
public static Date getDateAfter(Date baseDate, int day){
Calendar now = Calendar.getInstance();
now.setTime(baseDate);
now.set(Calendar.DATE, now.get(Calendar.DATE)+day);
return now.getTime();
}
/**
* 獲取指定日期爲星期幾,從星期日-星期六對應的是1-7
* @param date 指定日期
* @return 星期幾
*/
public static int getDayOfWeek(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
}