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

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

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);
}


}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值