You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
431 lines
13 KiB
431 lines
13 KiB
2 years ago
|
package com.bocom.api.helper;
|
||
|
|
||
|
import java.text.ParseException;
|
||
|
import java.text.ParsePosition;
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.Calendar;
|
||
|
import java.util.Date;
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* 时间操作工具
|
||
|
*/
|
||
|
public class DateHelper {
|
||
|
|
||
|
public enum DateEnum {
|
||
|
yyyy_MM_dd_HH_mm_ss("yyyy-MM-dd HH:mm:ss"),
|
||
|
yyyy_MM_dd("yyyy-MM-dd"),
|
||
|
HH_mm_ss("HH:mm:ss"),
|
||
|
yyyyMMddHHmmss("yyyyMMddHHmmss"),
|
||
|
yyyyMMdd("yyyyMMdd"),
|
||
|
HHmmss("HHmmss"),
|
||
|
yyyy("yyyy"),
|
||
|
MM("MM"),
|
||
|
dd("dd"),
|
||
|
HH("HH"),
|
||
|
mm("mm"),
|
||
|
ss("ss");
|
||
|
|
||
|
private final String type;
|
||
|
|
||
|
DateEnum(String type) {
|
||
|
this.type = type;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* [日期字符串]转日期/时间
|
||
|
*
|
||
|
* @param dateStr 日期字符串
|
||
|
* @param dateEnum 日期格式枚举
|
||
|
* @return 日期/时间
|
||
|
*/
|
||
|
public static Date strToDate(String dateStr, DateEnum dateEnum) {
|
||
|
return strToDate(dateStr, dateEnum.type);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* [日期字符串]转日期
|
||
|
*
|
||
|
* @param dateStr 日期字符串
|
||
|
* @param format 日期格式
|
||
|
* @return 日期/时间
|
||
|
*/
|
||
|
public static Date strToDate(String dateStr, String format) {
|
||
|
try {
|
||
|
return new SimpleDateFormat(format).parse(dateStr);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* [日期]转字符串
|
||
|
*
|
||
|
* @param date 日期
|
||
|
* @param dateEnum 日期格式枚举
|
||
|
* @return 日期字符串
|
||
|
*/
|
||
|
public static String dateToStr(Date date, DateEnum dateEnum) {
|
||
|
return dateToStr(date, dateEnum.type);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* [日期]转字符串
|
||
|
*
|
||
|
* @param date 日期
|
||
|
* @param format 日期格式
|
||
|
* @return 日期字符串
|
||
|
*/
|
||
|
public static String dateToStr(Date date, String format) {
|
||
|
try {
|
||
|
return new SimpleDateFormat(format).format(date);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 判断是否已超过[今天]的[指定时间点]
|
||
|
*
|
||
|
* @param time 指定时间点
|
||
|
*/
|
||
|
public static Boolean isOverTimeByToday(String time) {
|
||
|
return isOverTime(getCurDateTime(), time);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断是否已超过[指定日期]的[指定时间点]
|
||
|
*
|
||
|
* @param dateTime 指定日期
|
||
|
* @param time 指定时间点
|
||
|
*/
|
||
|
public static Boolean isOverTime(String dateTime, String time) {
|
||
|
try {
|
||
|
if (time == null || "".equals(time)
|
||
|
|| dateTime == null || "".equals(dateTime)) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
SimpleDateFormat format = new SimpleDateFormat(DateEnum.yyyy_MM_dd_HH_mm_ss.type);
|
||
|
time = getCurDate() + " " + time.trim();
|
||
|
long spotTime = format.parse(time).getTime(); // 比对时间点
|
||
|
long curTime = format.parse(dateTime).getTime(); // 当前时间点
|
||
|
return curTime > spotTime;
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取当前月的所有号数(1、2、3...)
|
||
|
*/
|
||
|
public static List<String> getCurMonthDays() {
|
||
|
List<String> days = new ArrayList<>();
|
||
|
|
||
|
try {
|
||
|
SimpleDateFormat yFormat = new SimpleDateFormat(DateEnum.yyyy.type);
|
||
|
SimpleDateFormat mFormat = new SimpleDateFormat(DateEnum.MM.type);
|
||
|
SimpleDateFormat dFormat = new SimpleDateFormat(DateEnum.dd.type);
|
||
|
Date date = new Date();
|
||
|
String y = yFormat.format(date);
|
||
|
String m = mFormat.format(date);
|
||
|
String d = dFormat.format(date);
|
||
|
|
||
|
for (int i = 1; i <= Integer.parseInt(d); i++) {
|
||
|
days.add(y + "-" + m + "-" + (i < 10 ? ("0" + i) : i));
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return days;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取当前具体日期和时间
|
||
|
*
|
||
|
* @return 具体日期和时间
|
||
|
*/
|
||
|
public static String getCurDateTime() {
|
||
|
SimpleDateFormat formatter = new SimpleDateFormat(DateEnum.yyyy_MM_dd_HH_mm_ss.type);
|
||
|
return formatter.format(new Date());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取当前时间
|
||
|
*
|
||
|
* @return 当前时间
|
||
|
*/
|
||
|
public static String getCurTime() {
|
||
|
SimpleDateFormat formatter = new SimpleDateFormat(DateEnum.HH_mm_ss.type);
|
||
|
return formatter.format(new Date());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 获取当前日期
|
||
|
*
|
||
|
* @return 当前日期
|
||
|
*/
|
||
|
public static String getCurDate() {
|
||
|
SimpleDateFormat formatter = new SimpleDateFormat(DateEnum.yyyy_MM_dd.type);
|
||
|
return formatter.format(new Date());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 得到一个时间延后或前移几天的时间
|
||
|
*
|
||
|
* @param date 指定日期
|
||
|
* @param moveDays 移动天数
|
||
|
* @return String 日期
|
||
|
*/
|
||
|
public static String getMoveDate(Date date, int moveDays) {
|
||
|
try {
|
||
|
SimpleDateFormat format = new SimpleDateFormat(DateEnum.yyyy_MM_dd.type);
|
||
|
return getMoveDate(format.format(date), moveDays, null);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 得到一个时间延后或前移几天的时间
|
||
|
*
|
||
|
* @param date 指定日期
|
||
|
* @param moveDays 移动天数
|
||
|
* @return String 日期
|
||
|
*/
|
||
|
public static String getMoveDateByType(Date date, int moveDays, DateEnum type) {
|
||
|
try {
|
||
|
SimpleDateFormat format = new SimpleDateFormat(type.type);
|
||
|
return getMoveDate(format.format(date), moveDays, type);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获得[指定日期]平移[moveDays]天的时间
|
||
|
*
|
||
|
* @param date 指定日期
|
||
|
* @param moveDays 移动天数(可为负)
|
||
|
* @return String 日期
|
||
|
*/
|
||
|
public static String getMoveDate(String date, long moveDays, DateEnum type) {
|
||
|
try {
|
||
|
if (date == null) {
|
||
|
return null;
|
||
|
}
|
||
|
if (type == null) {
|
||
|
type = DateEnum.yyyy_MM_dd;
|
||
|
}
|
||
|
SimpleDateFormat format = new SimpleDateFormat(type.type);
|
||
|
ParsePosition pos = new ParsePosition(0);
|
||
|
Date day = format.parse(date, pos);
|
||
|
long time = (day.getTime() / 1000) + (moveDays * 24 * 60 * 60);
|
||
|
day.setTime(time * 1000);
|
||
|
return format.format(day);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获得按[指定时间]平移[moveMinute]分钟得时间
|
||
|
*
|
||
|
* @param date 传入日期进行拼接(为空不拼接)
|
||
|
* @param dateTime 指定时间
|
||
|
* @param moveMinute 移动分钟(可为负数)
|
||
|
* @return 日期
|
||
|
*/
|
||
|
public static String getMoveTime(String date, String dateTime, long moveMinute) {
|
||
|
try {
|
||
|
if (dateTime == null) {
|
||
|
return null;
|
||
|
}
|
||
|
SimpleDateFormat format = new SimpleDateFormat(DateEnum.HH_mm_ss.type);
|
||
|
ParsePosition pos = new ParsePosition(0);
|
||
|
Date day = format.parse(dateTime, pos);
|
||
|
long time = (day.getTime() / 1000) + (moveMinute * 60);
|
||
|
day.setTime(time * 1000);
|
||
|
String comTime = format.format(day);
|
||
|
if (date != null) {
|
||
|
return date + " " + comTime;
|
||
|
}
|
||
|
return comTime;
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断对照日期是否为今天
|
||
|
*
|
||
|
* @param date 对照日期
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public static boolean isToday(String date) {
|
||
|
return inDateRange(date, date, null, DateEnum.yyyy_MM_dd.type);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 判断当前时间是否在[startTime, endTime]区间
|
||
|
*
|
||
|
* @param beginTime 开始时间
|
||
|
* @param endTime 结束时间
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public static boolean curTimeInTimeRange(String beginTime, String endTime) {
|
||
|
return inDateRange(beginTime, endTime, null, DateEnum.HH_mm_ss.type);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断[今天]判断对照时间是否在[begDate, endDate]区间
|
||
|
*
|
||
|
* @param begDate 开始日期
|
||
|
* @param endDate 结束日期
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public static boolean todayInDateRange(String begDate, String endDate) {
|
||
|
return inDateRange(begDate, endDate, null, DateEnum.yyyy_MM_dd.type);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断对照时间是否在[startTime, endTime]区间
|
||
|
*
|
||
|
* @param begTime 开始时间
|
||
|
* @param endTime 结束时间
|
||
|
* @param compareTime 对照时间
|
||
|
* @param dateEnum 时间格式枚举
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public static boolean inDateRange(String begTime, String endTime, String compareTime, DateEnum dateEnum) {
|
||
|
return inDateRange(begTime, endTime, compareTime, dateEnum.type);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断对照时间是否在[startTime, endTime]区间
|
||
|
*
|
||
|
* @param begTime 开始时间
|
||
|
* @param endTime 结束时间
|
||
|
* @param compareTime 对照时间
|
||
|
* @param format 时间格式
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public static boolean inDateRange(String begTime, String endTime, String compareTime, String format) {
|
||
|
if (begTime == null || endTime == null || format == null
|
||
|
|| begTime.length() != format.length() || endTime.length() != format.length()) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
||
|
if (compareTime == null) {
|
||
|
compareTime = sdf.format(new Date());
|
||
|
}
|
||
|
Date begin = sdf.parse(begTime);
|
||
|
Date end = sdf.parse(endTime);
|
||
|
Date now = sdf.parse(compareTime);
|
||
|
|
||
|
if (now.getTime() == begin.getTime() || now.getTime() == end.getTime()) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
Calendar nowCalendar = Calendar.getInstance();
|
||
|
nowCalendar.setTime(now);
|
||
|
|
||
|
Calendar beginCalendar = Calendar.getInstance();
|
||
|
beginCalendar.setTime(begin);
|
||
|
|
||
|
Calendar endCalendar = Calendar.getInstance();
|
||
|
endCalendar.setTime(end);
|
||
|
return nowCalendar.after(beginCalendar) && nowCalendar.before(endCalendar);
|
||
|
|
||
|
} catch (ParseException e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 计算两个日期间隔天数
|
||
|
*
|
||
|
* @param begDateStr 开始日期
|
||
|
* @param endDateStr 结束日期
|
||
|
* @param isInclude 是否包含起止日期
|
||
|
* @return 间隔天数
|
||
|
*/
|
||
|
public static int intervalDays(String begDateStr, String endDateStr, boolean isInclude) {
|
||
|
if (begDateStr == null || endDateStr == null) {
|
||
|
return -1;
|
||
|
}
|
||
|
try {
|
||
|
SimpleDateFormat format = new SimpleDateFormat(DateEnum.yyyy_MM_dd.type);
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
|
||
|
calendar.setTime(format.parse(begDateStr));
|
||
|
long begTime = calendar.getTimeInMillis();
|
||
|
|
||
|
calendar.setTime(format.parse(endDateStr));
|
||
|
long endTime = calendar.getTimeInMillis();
|
||
|
|
||
|
long between_days;
|
||
|
if (begTime == endTime) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (begTime < endTime) {
|
||
|
between_days = (endTime - begTime) / (1000 * 3600 * 24);
|
||
|
} else {
|
||
|
between_days = (begTime - endTime) / (1000 * 3600 * 24);
|
||
|
}
|
||
|
return Integer.parseInt(String.valueOf(between_days)) + (isInclude ? 1 : -1);
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 根据一个日期,返回是星期几的字符串
|
||
|
*
|
||
|
* @param dateStr 时间字符串
|
||
|
* @return 星期
|
||
|
*/
|
||
|
public static String getWeek(String dateStr) {
|
||
|
try {
|
||
|
Date date = DateHelper.strToDate(dateStr, DateEnum.yyyy_MM_dd);
|
||
|
if (date == null) {
|
||
|
return null;
|
||
|
}
|
||
|
Calendar calendar = Calendar.getInstance();
|
||
|
calendar.setTime(date);
|
||
|
// int hour = c.get(Calendar.DAY_OF_WEEK);
|
||
|
// hour中存的就是星期几了,其范围 1~7
|
||
|
// 1=星期日 7=星期六,其他类推
|
||
|
return new SimpleDateFormat("EEEE").format(calendar.getTime());
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|