package com.yssh.utils;
|
|
import java.lang.management.ManagementFactory;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
/**
|
* 时间工具类
|
*
|
* @author tam
|
*/
|
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
public static String YYYY = "yyyy";
|
|
public static String YYYY_MM = "yyyy-MM";
|
|
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
|
public static String YYYYMMDDHH = "yyyyMMddHH";
|
|
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
|
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
|
|
public static String YYYY_MM_DD_HH = "yyyy-MM-dd HH";
|
|
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
|
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
|
"yyyy.MM.dd HH:mm", "yyyy.MM" };
|
|
/**
|
* 获取当前Date型日期
|
*
|
* @return Date() 当前日期
|
*/
|
public static Date getNowDate() {
|
return new Date();
|
}
|
|
/**
|
* 获取当前日期, 默认格式为yyyy-MM-dd
|
*
|
* @return String
|
*/
|
public static String getDate() {
|
return dateTimeNow(YYYY_MM_DD);
|
}
|
|
public static final String getTime() {
|
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
}
|
|
public static final String dateTimeNow() {
|
return dateTimeNow(YYYYMMDDHHMMSS);
|
}
|
|
public static final String dateTimeNow(final String format) {
|
return parseDateToStr(format, new Date());
|
}
|
|
public static final String dateTime(final Date date) {
|
return parseDateToStr(YYYY_MM_DD, date);
|
}
|
|
public static final String parseDateToStr(final String format, final Date date) {
|
return new SimpleDateFormat(format).format(date);
|
}
|
|
public static final Date dateTime(final String format, final String ts) {
|
try {
|
return new SimpleDateFormat(format).parse(ts);
|
} catch (ParseException e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
public static String getYyyyMmDdHh(Date date) {
|
return new SimpleDateFormat(YYYYMMDDHH).format(date);
|
}
|
|
public static String getYyyyMmDdHhMmSs(Date date) {
|
return new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS).format(date);
|
}
|
|
/**
|
* 日期路径 即年/月/日 如2018/08/08
|
*/
|
public static final String datePath() {
|
Date now = new Date();
|
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
}
|
|
/**
|
* 日期路径 即年/月/日 如20180808
|
*/
|
public static final String dateTime() {
|
Date now = new Date();
|
return DateFormatUtils.format(now, "yyyyMMdd");
|
}
|
|
/**
|
* 日期型字符串转化为日期 格式
|
*/
|
public static Date parseDate(Object str) {
|
if (str == null) {
|
return null;
|
}
|
try {
|
return parseDate(str.toString(), parsePatterns);
|
} catch (ParseException e) {
|
return null;
|
}
|
}
|
|
/**
|
* 获取服务器启动时间
|
*/
|
public static Date getServerStartDate() {
|
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
|
return new Date(time);
|
}
|
|
/**
|
* 计算两个时间差
|
*/
|
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 % nd % nh / nm;
|
// 计算差多少秒//输出结果
|
// long sec = diff % nd % nh % nm / ns;
|
return day + "天" + hour + "小时" + min + "分钟";
|
}
|
|
/**
|
* @Title: differHour
|
* @Description: 获取两个时间差几个小时
|
* @param: @param endDate
|
* @param: @param nowDate
|
* @param: @return
|
* @return: long
|
* @throws
|
*/
|
public static int differDay(Date startDate, Date endDate){
|
long nd = 1000 * 24 * 60 * 60;
|
long diff = endDate.getTime() - startDate.getTime();
|
double dd = Double.valueOf(String.valueOf(diff)) / nd;
|
// 计算差多少天
|
return Double.valueOf(Math.ceil(dd)).intValue();
|
}
|
|
/**
|
* @Description: 获取一段时间间隔
|
* @param:date 需要操作的时间对象
|
* @param:day 操作的时间间隔
|
* 正数为之后一段时间
|
* 负数为之前一段时间
|
* @param:dateType 操作的时间时分秒天选择
|
* 常用有:
|
* Calendar.MINUTE 分
|
* Calendar.HOUR_OF_DAY 时
|
* Calendar.DATE 天
|
* Calendar.MONTH 月
|
*/
|
public static Date getAPeriodOfTime(Date date, int day,int dateType) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
calendar.add(dateType, day);
|
return calendar.getTime();
|
}
|
|
public static List<String> get3Hours() {
|
List<String> times = new ArrayList<>();
|
SimpleDateFormat ymdh = new SimpleDateFormat(YYYYMMDDHH);
|
|
Calendar calendar = Calendar.getInstance();
|
calendar.add(Calendar.HOUR_OF_DAY, 1);
|
|
for (int i = 0; i < 3; i++) {
|
calendar.add(Calendar.HOUR_OF_DAY, -1);
|
String time = ymdh.format(calendar.getTime());
|
// times.add(time);
|
times.add(0, time);
|
}
|
|
return times;
|
}
|
|
/**
|
*/
|
public static Map<Date, Date> segmentationDateByTimeQuantum(Date startDate, Date endDate, int timeQuantum,int dateType){
|
LinkedHashMap<Date, Date> result = new LinkedHashMap<Date, Date>();
|
long startTimeMillisecond= startDate.getTime();
|
long endTimeMillisecond = endDate.getTime();
|
Long quantumTimeMillisecond = 0L;
|
switch (dateType) {
|
case Calendar.DATE:
|
quantumTimeMillisecond = new Integer(1000 * 60 * 60 * 24 * timeQuantum).longValue();
|
break;
|
case Calendar.HOUR_OF_DAY:
|
quantumTimeMillisecond = new Integer(1000 * 60 * 60 * timeQuantum).longValue();
|
break;
|
case Calendar.MINUTE:
|
quantumTimeMillisecond = new Integer(1000 * 60 * timeQuantum).longValue();
|
break;
|
default:
|
break;
|
}
|
for (long time = startTimeMillisecond; time < endTimeMillisecond; time += quantumTimeMillisecond) {
|
Date beginDate = new Date(time);
|
Date finishDate = new Date(time + quantumTimeMillisecond);
|
if (finishDate.getTime() > endTimeMillisecond) {
|
finishDate = endDate;
|
}
|
result.put(beginDate, finishDate);
|
}
|
return result;
|
}
|
|
/**
|
* 获取本月第一天
|
* @return String
|
**/
|
public static Date getMonthStart() {
|
Calendar cal = Calendar.getInstance();
|
//cal.set(Calendar.MONTH, 3);
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
return dateTime(YYYY_MM_DD_HH_MM_SS, parseDateToStr(YYYY_MM_DD, cal.getTime()) + " 00:00:00");
|
}
|
|
/**
|
* 获取本月最后一天
|
* @return String
|
**/
|
public static Date getMonthEnd() {
|
Calendar cal = Calendar.getInstance();
|
//cal.set(Calendar.MONTH, 3);
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
return dateTime(YYYY_MM_DD_HH_MM_SS, parseDateToStr(YYYY_MM_DD, cal.getTime()) + " 23:59:59");
|
}
|
|
public static void main(String[] args) {
|
//System.out.println(parseDateToStr(YYYY_MM_DD_HH_MM_SS, getMonthStart()));
|
//System.err.println(parseDateToStr(YYYY_MM_DD_HH_MM_SS, getMonthEnd()));
|
System.out.println(parseDateToStr(YYYY_MM_DD_HH_MM_SS, getAPeriodOfTime(getNowDate(), -7, Calendar.DATE)));
|
}
|
}
|