isHoliday() 方法判斷傳入日期是否為六日
getDuration() 方法回傳相減過後的秒數,包含六日
getTimeFrame() 方法回傳相減過後的秒數,不包含六日
/**
* Check holiday
*
* @param startTime
* @param endTime
* @return true or false
*/
public static boolean isHoliday(Calendar
calander) {
boolean isHoliday = false;
if
(calander.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
&& calander.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
isHoliday =
false;
} else {
isHoliday =
true;
}
return isHoliday;
}
/**
* Get the total
seconds between the startTime & endTime
*
* @param startTime
* @param endTime
* @return
*/
public static long getDuration(Date
startTime, Date endTime) {
long totalSeconds = 0;
Calendar
startCal = Calendar.getInstance();
startCal.setTime(startTime);
Calendar endCal
= Calendar.getInstance();
endCal.setTime(endTime);
totalSeconds =
(endCal.getTimeInMillis() - startCal.getTimeInMillis()) / 1000;
return totalSeconds;
}
/**
* Get the total
seconds between the startTime & endTime (Without Sunday &
* Saturday)
*
* @param startTime
* @param endTime
* @return
*/
public static long getTimeFrame(Date
startTime, Date endTime) {
long timeFrame = 0;
Calendar
startCal = Calendar.getInstance();
startCal.setTime(startTime);
//startCal.add(Calendar.DATE, -6);
Calendar endCal
= Calendar.getInstance();
endCal.setTime(endTime);
//endCal.add(Calendar.DATE, 1);
int useDays = 0;
long totalSeconds = 0;
Calendar
tmpDate = Calendar.getInstance();
tmpDate.setTimeInMillis(startCal.getTimeInMillis());
if
(DateUtils.isSameDate(startCal, endCal) && startCal.before(endCal)) {
useDays =
0;
totalSeconds = (endCal.getTimeInMillis() - startCal.getTimeInMillis()) /
1000;
return totalSeconds;
} else if
(!DateUtils.isSameDate(startCal, endCal) && startCal.before(endCal)) {
long
secondsPastOfEndTime = endCal.get(Calendar.SECOND) +
endCal.get(Calendar.MINUTE) * 60
+
endCal.get(Calendar.HOUR_OF_DAY) * 60 * 60;
long
secondsRemainOfStartTime = 0;
while (tmpDate.before(endCal))
{
tmpDate.add(Calendar.DATE, 1);
if
(DateUtils.isSameDate(tmpDate, endCal)) {
break;
} else {
if
(!MxDateUtility.isHoliday(startCal) && MxDateUtility.isHoliday(endCal))
{
useDays++;
} else {
if (tmpDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
&&
tmpDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
useDays++;
}
}
}
}
if
(MxDateUtility.isHoliday(startCal) && !MxDateUtility.isHoliday(endCal))
{
secondsRemainOfStartTime = 0;
} else {
secondsRemainOfStartTime = 86400 - (startCal.get(Calendar.SECOND) +
startCal.get(Calendar.MINUTE) * 60 + startCal
.get(Calendar.HOUR_OF_DAY) * 60 * 60);
}
long secondsOfUseDays =
useDays * 86400;
totalSeconds
= secondsRemainOfStartTime + secondsOfUseDays + secondsPastOfEndTime;
return totalSeconds;
} else {
return 0;
}
}
沒有留言:
張貼留言