由于工作上用到发送上一个月,上一周这中获取时间的部分,所以找了个工具类记录下
/**
* 计算两个时间相隔的天数
* @param date1
* @param date2
* @return
*/
public static int differentDaysByMillisecond(Date date1, Date date2) {
try{
return (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
}catch (Exception e){
return 0;
}
}
/**
* 计算两个时间相隔的天数
* @param date1
* @param date2
* @return
*/
public static float differentHoursByMillisecond(Date date1, Date date2) {
float hours = (float) ((date2.getTime() - date1.getTime()) / (1000 * 3600.0));
return hours;
}
/**
* 判断日期是否为节假日
*
* @return
* @throws ApiException
*/
public static Integer isWork(String time) throws ParseException {
String httpArg = dateToWork(time);
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
Integer data = null;
try {
URL url = new URL(CHECKISWORKDAY + httpArg);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
Map<String, Object> parse = (Map) JSON.parse(result);
data = (Integer) parse.get("data");
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
/**
* 计算小时
*
* @param on
* @param off
* @return
*/
public static double dateToDouble(Date on, Date off) {
long l = off.getTime() - on.getTime();//毫秒
// long l1 = l / 1000;//秒
// long l2 = l1 / 60;//分钟
// long l3 = l2 / 60;//小时
return l / 1000 / 60 / 60.0;
}
/**
* 判断是否是工作日的格式
*
* @return
*/
public static String dateToWork(String time) throws ParseException {
Date date = stringToDate(time);
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
return f.format(date);
}
/**
* 获取昨天的时间
*
* @return
*/
public static String getYesterday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
Date time = cal.getTime();
String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time);
return format;
}
/**
* 获取现在的时间
*
* @return
*/
public static String dataToString(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Date time = cal.getTime();
String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time);
return format;
}
/**
* 字符串转date
*/
public static Date stringToDate(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.parse(time);
}
/**
* 字符串转date2
*/
public static Date stringToDate2(String time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return sdf.parse(time);
}
/**
* 获取昨天上班的时间
*
* @return
*/
public static Date getYesterdayOnDutyTime(Date time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar date = Calendar.getInstance();
date.setTime(time);
date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
String yesterdayOnDutyTime = sdf.format(date.getTime()) + " 09:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return df.parse(yesterdayOnDutyTime);
}
/**
* 获取零点时间
*
* @return
*/
public static Date getTimeZero(Date time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar date = Calendar.getInstance();
date.setTime(time);
date.set(Calendar.DATE, date.get(Calendar.DATE));
String yesterdayOnDutyTime = sdf.format(date.getTime()) + " 00:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return df.parse(yesterdayOnDutyTime);
}
/**
* 获取7天前上班的时间
*
* @return
*/
public static Date getDutyTimeLastWork(Date time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar date = Calendar.getInstance();
date.setTime(time);
date.set(Calendar.DATE, date.get(Calendar.DATE) - 7);
String yesterdayOnDutyTime = sdf.format(date.getTime()) + " 09:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return df.parse(yesterdayOnDutyTime);
}
/**
* 获取昨天下班的时间
*
* @return
*/
public static Date getYesterdayOffDutyTime(Date time) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar date = Calendar.getInstance();
date.setTime(time);
date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
String yesterdayOnDutyTime = sdf.format(date.getTime()) + " 18:00:00";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return df.parse(yesterdayOnDutyTime);
}
/**
* 天数加一
*/
public static String getNextDay(String time) throws ParseException {
Date date = stringToDate(time);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, 1);
Date time1 = cal.getTime();
String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time1);
return format;
}