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);
}
}
- Android时间平移以及计算时间差,以及计算工作日
- C#中几种比较时间以及计算时间差的方法
- iOS 获取当前时间以及计算年龄(时间差)
- iOS 获取当前时间以及计算年龄(时间差)
- java Calendar类时间操作简单总结,计算工作日排除法定假日以及周末
- Java 计算时间差以及比较日期大小
- linux内核计算时间差以及jiffies溢出
- java计算工作日,排除周末以及法定假日
- CString 时间字符串、COleDateTime 和 CTime 类之间的转换以及如何计算时间差
- php计算时间差,时间比较,时间计算
- 线段长度计算以及相交、平移和旋转算法
- Android计算时间差
- Android Java计算时间差
- Android中计算时间差
- Android如何计算时间差
- Android如何计算时间差
- Android计算时间差
- 工作日计算
- [leetcode] 【数组】 89. Gray Code
- Apache与Nginx的区别
- I2C总线原理及应用实例
- Ehcache Memcache Redis 初步 (一)
- 分页关键知识点总结
- Android时间平移以及计算时间差,以及计算工作日
- 《CSS权威指南》学习记录——特殊性和层叠
- 肿瘤检测
- 详细记录python的range()函数用法
- YUM介绍
- 漂亮回答面试官struts2的原理
- Android Studio 开发中碰到的问题总结
- 使用py-faster-rcnn训练自己的数据
- 【New AKOJ】最左边一位数