Android时间平移以及计算时间差,以及计算工作日

来源:互联网 发布:淘宝客服需要什么学历 编辑:程序博客网 时间:2024/06/05 14:18

  最近由于项目需要对时间进行大量的计算,刚开始不知道计算工作日,就去遍历了时间差,然后判断每一天是不是在工作日,结果是出来,但是总觉得怪怪的,老大看了之后,说为什么不用公试去计算呢,你这样不行,想想也是,不多说了,看看代码,我将复制我整个时间工具类!!!!!

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;


import android.annotation.SuppressLint;


public class TimeUtils {
/**
* 指定时间 分钟 向前 或 向后 平移

* @param date
* @param dayNum
* @return
*/
public static Date setDateMinute(Date date, int minute) {
GregorianCalendar now = new GregorianCalendar();
now.setTime(date);
now.add(GregorianCalendar.MINUTE, minute);
return now.getTime();
}


/**
* 指定时间 天 向前 或 向后 平移

* @param date
* @param dayNum
* @return
*/
public static Date setDateDay(Date date, int dayNum) {
GregorianCalendar now = new GregorianCalendar();
now.setTime(date);
now.add(GregorianCalendar.DATE, dayNum);
return now.getTime();
}


/**
* 把字符串转为小时

* @param strDate
* @return
* @throws Exception
*/
@SuppressLint("SimpleDateFormat")
public static Date ConverToDate(String strDate) throws Exception {
DateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.parse(strDate);
}


/**
* 把字符串转为date

* @param strDate
* @return
* @throws Exception
*/
@SuppressLint("SimpleDateFormat")
public static Date StringToDate(String strDate) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.parse(strDate);
}


@SuppressLint("SimpleDateFormat")
public static String ConverToString(Date date) {
DateFormat df = new SimpleDateFormat("HH:mm:ss");
return df.format(date);
}
@SuppressLint("SimpleDateFormat")
public static String dateToString(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}


/**
* 判断两时间的之差是否大于某一个时间

* @param string
* @param charSequence
* @param i
* @return
* @throws Exception
*/
@SuppressLint("SimpleDateFormat")

public static Boolean CompareTime(String string, String charSequence, long i)
throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = df.parse(string);
Date date = df.parse(charSequence);

               // i就是你判断时间差大于一个数,
if ((now.getTime() - date.getTime()) / (24 * 60 * 60 * 1000) >= i) {
return true;
} else {
return false;
}


}

         //计算时间差
@SuppressLint("SimpleDateFormat")
public static int compareTimeResult(String startDate, String endDate)
throws Exception {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = df.parse(startDate);
Date date = df.parse(endDate);
return (int) ((date.getTime() - now.getTime()) / (24 * 60 * 60 * 1000));
}


// /**
// * 计算两个时间的周末天数
//
// * @return
// */
// @SuppressLint("SimpleDateFormat")

        //刚开始的笨办法
// public static int isWeekend(String startDate, String endDate)
// throws Exception {
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// Date now = df.parse(startDate);
// Date date = df.parse(endDate);
// int i;
// if (now.getDay() == 1 || now.getDay() == 2 || now.getDay() == 3
// || now.getDay() == 4 || now.getDay() == 5) {
// i = 1;
// } else {
// i = 0;
// }
//
// int subdate = (int) ((date.getTime() - now.getTime()) / (24 * 60 * 60 * 1000));
// for (int j = 0; j < subdate; j++) {
// now = TimeUtils.setDateDay(now, 1);
// if (now.getDay() == 1 || now.getDay() == 2 || now.getDay() == 3
// || now.getDay() == 4 || now.getDay() == 5) {
// i++;
// }// 判断是不是双休日
// }
// return i;
//
// }


/**
* 计算两个时间的工作日

* @param startDate
* @param endDate
* @return
* @throws Exception
*/
public static int isWeekend(String startDate, String endDate)
throws Exception {
int thisweek, lastweek;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = df.parse(startDate);
Date date = df.parse(endDate);
int subdate = (int) ((date.getTime() - now.getTime()) / (24 * 60 * 60 * 1000));
int nowtoday = now.getDay();
int datetoday = date.getDay();
if (nowtoday == 0 || nowtoday == 6) {
thisweek = 5;
} else {
thisweek = 7 - nowtoday - 2;
}
if (datetoday == 0 || datetoday == 6) {
lastweek = 0;
} else {
lastweek = datetoday;
}
int i;
if (nowtoday == 0 || nowtoday == 6) {
i = (subdate + 1 - thisweek - lastweek) / 7 * 5 + thisweek
+ lastweek;
} else {
i = (subdate + 1 - thisweek - lastweek) / 7 * 5 + thisweek
+ lastweek + 1;
}
return i;


}
/**
* 给定一个日期加天数得到除工作日的日期
* @param strDate
* @param dayNum
* @return
* @throws Exception
*/
public static Date setWorkingDate(String strDate,int dayNum) throws Exception{
int thisweek;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date now = df.parse(strDate);
int nowtoday = now.getDay();
int weekday=0;
if (nowtoday == 0) {
thisweek = 1;
}else if(nowtoday==6){
thisweek = 2;
} else {
thisweek = 7 - nowtoday+1;
weekday=thisweek-2;
}
int i=(dayNum-weekday)/5*7+(dayNum-weekday)%5+thisweek;
return TimeUtils.setDateDay(now, i-3);
}


}

3 0
原创粉丝点击