/**
* 计算两个时间相差多少天,多少小时,多少分钟
* @param endDate
* @param nowDate
* @return
*/
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// // 计算差多少天
long day = diff / nd;
// // 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
/**
* 判断2个时间相差多少分
* @param begin 开始时间
* @param end 结束时间
* @return
* @Exception
*/
public static Long getMinNum(Date begin, Date end) {
long between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒
long min = between / 60;
return min;
}
/**
* 获取两个日期之间的小时数
*
* @param start 开始时间
* @param end 结束时间
* @return
* @throws ParseException
*/
public static Long longTimeToDay(Date start, Date end) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//获取毫秒数
Long startLong = start.getTime();
Long endLong = end.getTime();
//计算时间差,单位毫秒
Long ms = endLong - startLong;
Integer ss = 1000;
Integer mi = ss * 60;
Integer hh = mi * 60;
Integer dd = hh * 24;
Long day = ms / dd;
Long hour = day * 24 + (ms - day * dd) / hh;
StringBuffer sb = new StringBuffer();
return hour;
}
/**
* 获取当前日期上一周开始时间、结束时间和年份
*
* @param dateParam 开始时间
* @return
*/
public static Map<String, Object> getParams(Date dateParam) {
Map<String, Object> map = new HashMap<String, Object>();
SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sbf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 当前年份
int year = 0;
// 年的周数
int week_index = 0;
// 周开始时间
Date start = null;
// 周结束时间
Date end = null;
try {
cal.setTime(dateParam);
year = cal.get(Calendar.YEAR);
week_index = cal.get(Calendar.WEEK_OF_YEAR);
Date date = null;
String s = "";
// 获取上周周一的开始时间
cal.add(Calendar.WEEK_OF_YEAR, -1);
// 设置日期为周一
cal.set(Calendar.DAY_OF_WEEK, 2);
date = cal.getTime();
s = sbf.format(date) + " 00:00:00";
start = sbf1.parse(s);
// 获取上周周日结束时间
cal.add(Calendar.WEEK_OF_YEAR, 1);
// 设置日期为周日
cal.set(Calendar.DAY_OF_WEEK, 1);
date = cal.getTime();
s = sbf.format(date) + " 23:59:59";
end = sbf1.parse(s);
} catch (Exception e) {
LOGGER.info(e.getMessage(), e);
}
map.put("year", year);
map.put("week_index", week_index);
map.put("week_start", start);
map.put("week_end", end);
return map;
}
/**
* 获取当前日期上一周开始时间
*
* @param starDate 开始时间
* @return
*/
public static Date getBeginDayOfLastWeek(Date starDate) {
SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sbf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 周开始时间
Date start = null;
// 周结束时间
try {
cal.setTime(starDate);
Date date = null;
String s = "";
// 获取上周周一的开始时间
cal.add(Calendar.WEEK_OF_YEAR, -1);
// 设置日期为周一
cal.set(Calendar.DAY_OF_WEEK, 2);
date = cal.getTime();
s = sbf.format(date) + " 00:00:00";
start = sbf1.parse(s);
} catch (Exception e) {
LOGGER.info(e.getMessage(), e);
}
return start;
}
/**
* 获取当前日期上一周结束时间
*
* @param starDate 开始时间
* @return
*/
public static Date getEndDayOfLastWeek(Date starDate) {
SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sbf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 周开始时间
Date start = null;
// 周结束时间
Date end = null;
try {
cal.setTime(starDate);
Date date = null;
String s = "";
// 获取上周周一的开始时间
cal.add(Calendar.WEEK_OF_YEAR, -1);
// 设置日期为周一
cal.set(Calendar.DAY_OF_WEEK, 2);
date = cal.getTime();
s = sbf.format(date) + " 00:00:00";
start = sbf1.parse(s);
// 获取上周周日结束时间
cal.add(Calendar.WEEK_OF_YEAR, 1);
// 设置日期为周日
cal.set(Calendar.DAY_OF_WEEK, 1);
date = cal.getTime();
s = sbf.format(date) + " 23:59:59";
end = sbf1.parse(s);
} catch (Exception e) {
LOGGER.info(e.getMessage(), e);
}
return end;
}
/**
* 获取当前日期是星期几<br>
*
* @param date
* @return 当前日期是星期几
*/
public static String getWeekOfDate(Date date) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
/**
* 判断时间是否在时间段内
*
* @param nowTime
* @param beginTime
* @param endTime
* @return
*/
public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 获得当前月--开始日期
*
* @return
*/
public static String getMinMonthDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
}
/**
* 获得当前月--结束日期
*
* @return
*/
public static String getMaxMonthDate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return dateFormat.format(calendar.getTime());
}
常用的时间类集合DateUtil
最新推荐文章于 2024-04-28 05:27:48 发布