package com.tuantuan.common.utils;
import com.tuantuan.common.exception.BusinessException;
import org.springframework.util.ObjectUtils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author: wangqinmin
* @date: 2019/10/30
* @description: 仰天大笑出门去,我辈岂是蓬蒿人
* <p></p>
* 使用Date 、Calendar操作时间
*/
public class DateUtil {
/**
* 自定义常量
*/
public static final String formatStr_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String formatStr_YYYY_MM_DD_HH_MM_SS_S = "yyyy-MM-dd HH:mm:ss.S";
public static final String formatStr_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String formatStr_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String formatStr_YYYY_MM_DD_HH = "yyyy-MM-dd HH";
public static final String formatStr_YYYY_MM_DD = "yyyy-MM-dd";
public static final String formatStr_YYYYMMDD = "yyyyMMdd";
public static final String formatStr_HH_MM_SS = "HH:mm:ss";
public static final String formatStr_HH_MM = "HH:mm";
public static final String formatStr_YYYY = "yyyy";
public static final String formatStr_MM = "MM";
public static final String formatStr_DD = "dd";
public static final String formatStr_Hours = "HH";
public static final String formatStr_Minute = "mm";
public static final String formatStr_Second = "ss";
public static final String Line = "-";
public static final String COLON = ":";
/**
* ============================================= 字符串转时间工具 =================================================
*/
/**
* 根据String类型日期,返回date类,自定义时间格式
*
* @param dateStr
* @return
*/
public static Date getDateTime(String dateStr, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 字符串转日期
*
* @param str
* @return
* @throws ParseException
*/
public static Date StringformateToDate(String str) throws ParseException {
return ObjectUtils.isEmpty(str) ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS).parse(str);
}
/**
* ============================================= 时间转字符串工具 =================================================
*/
/**
* 根据Date获取自定义时间字符串
*
* @param date
* @return
*/
public static String getUserDefined(Date date, String formatStr) {
return date == null ? null : new SimpleDateFormat(formatStr).format(date);
}
/**
* @param date
* @return 年月日时分秒 20191021234559
*/
public static String getYyyyMmDdHhMmSs(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYYMMDDHHMMSS).format(date);
}
/**
* @param date
* @return 年-月-日 时:分:秒.毫秒 2019-10-21 23:59:59.99
*/
public static String getYyyy_Mm_Dd_Hh_Mm_Ss_s(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS_S).format(date);
}
/**
* @param date
* @return 年-月-日 时:分:秒 2019-10-21 23:59:59
*/
public static String getYyyy_Mm_Dd_Hh_Mm_Ss(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS).format(date);
}
/**
* @param date
* @return 年-月-日 时:分 2019-10-21 23:59
*/
public static String getYyyy_Mm_Dd_Hh_Mm(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM).format(date);
}
/**
* @param date
* @return 年-月-日 时 2019-10-21 23
*/
public static String getYyyy_Mm_Dd_Hh(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD_HH).format(date);
}
/**
* @param date
* @return 年-月-日 2019-10-21
*/
public static String getYyyy_Mm_Dd(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY_MM_DD).format(date);
}
/**
* @param date
* @return 年月日
*/
public static String getYyyyMmDd(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYYMMDD).format(date);
}
/**
* @param date
* @return 时:分:秒 23:59:59
*/
public static String getHh_Mm_Ss(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_HH_MM_SS).format(date);
}
/**
* @param date
* @return 时:分 23:59
*/
public static String getHh_Mm(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_HH_MM).format(date);
}
/**
* @param date
* @return 年
*/
public static String getYyyy(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_YYYY).format(date);
}
/**
* @param date
* @return 月
*/
public static String getMonth(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_MM).format(date);
}
/**
* @param date
* @return 天
*/
public static String getDay(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_DD).format(date);
}
/**
* @param date
* @return 时
*/
public static String getHours(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_Hours).format(date);
}
/**
* @param date
* @return 分
*/
public static String getMinute(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_Minute).format(date);
}
/**
* @param date
* @return 秒
*/
public static String getSecond(Date date) {
return date == null ? null : new SimpleDateFormat(formatStr_Second).format(date);
}
/**
* @return 当前时间, 日期格式为"yyyy-MM-dd HH:mm:ss"
*/
public static String getCurrentYYYY_MM_DD_HH_MM_SS() {
return new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS).format(new Date());
}
/**
* @return 使用Calendar类, 获取当前时间
*/
public static String getCurrentCalendarYYYY_MM_DD_HH_MM_SS() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS);
return sdf.format(calendar.getTime());
}
/**
* @return 使用Calendar类, 获取指定时间字符串显示 日期格式为"yyyy-MM-dd HH:mm:ss"
*/
public static String getCurrentCalendarYYYY_MM_DD_HH_MM_SS(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
SimpleDateFormat sdf = new SimpleDateFormat(formatStr_YYYY_MM_DD_HH_MM_SS);
return sdf.format(calendar.getTime());
}
/**
* @return 当前年
*/
public static String getCurrentYear() {
return new SimpleDateFormat(formatStr_YYYY).format(new Timestamp(System.currentTimeMillis()));
}
/**
* @return 当前月
*/
public static String getCurrentMonth() {
return new SimpleDateFormat(formatStr_MM).format(new Timestamp(System.currentTimeMillis()));
}
/**
* @return 当前日
*/
public static String getCurrentDay() {
return new SimpleDateFormat(formatStr_DD).format(new Timestamp(System.currentTimeMillis()));
}
/**
* @return 当前小时
*/
public static String getCurrentHour() {
return new SimpleDateFormat(formatStr_Hours).format(new Timestamp(System.currentTimeMillis()));
}
/**
* @return 当前分钟
*/
public static String getCurrentMinute() {
return new SimpleDateFormat(formatStr_Minute).format(new Timestamp(System.currentTimeMillis()));
}
/**
* @return 当前秒
*/
public static String getCurrentSecond() {
return new SimpleDateFormat(formatStr_Second).format(new Timestamp(System.currentTimeMillis()));
}
/**
* =============================================== 时间计算工具 ===================================================
*/
/**
* 月份相加
* month>0增加,month<0减少
*
* @param startDate
* @param month
* @return
*/
public static Date getDateAddMonth(Date startDate, int month) {
Calendar cal = getCalendar(startDate);
cal.add(Calendar.MONTH, month);
return cal.getTime();
}
/**
* 天数相加
* day>0增加,day<0减少
*
* @param day
* @param date
* @return
*/
public static Date getDateAddDay(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//指定日期向后推n天
calendar.add(Calendar.DAY_OF_MONTH, day);
return calendar.getTime();
}
/**
* 两时间月份差
*
* @param date1 传入时间不分前后
* @param date2 传入数据不分前后
* @return
*/
public static Integer getDateMonthDifference(Date date1, Date date2) {
Calendar bef = getCalendar(date1);
Calendar aft = getCalendar(date2);
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
return java.lang.Math.abs(result + month);
}
/**
这里有个年份上的bug,有时间再改,改bug为 大概好像是: 2020-12月 到 2021年12 中间差了 1年350多天的问题。
* 两时间相减 -- 获取时间长度
* 例如:
* 1年34天5小时5分钟4秒
* 34天5小时5分钟4秒
* 5小时5分钟4秒
*
* @param date1 传入时间不分前后
* @param date2 传入时间不分前后
* @return
*/
public static String getDateTimeSubtractTime(Date date1, Date date2) {
//这样得到的差值是毫秒级别
long diff = java.lang.Math.abs(date1.getTime() - date2.getTime());
// 获取年份差
Calendar from = Calendar.getInstance();
from.setTime(date1);
Calendar to = Calendar.getInstance();
to.setTime(date2);
int year = java.lang.Math.abs(from.get(Calendar.YEAR) - to.get(Calendar.YEAR));
// 天数
long days = diff / (1000 * 60 * 60 * 24);
long days_day = days % 365;
// 将小时转为天
long hours = diff / (1000 * 60 * 60);
long hous_day = hours % 24;
// 分钟
long minutes = (diff - hours * 1000 * 60 * 60) / 1000 / 60;
// 分钟
long second = (diff - hours * 1000 * 60 * 60 - minutes * 1000 * 60) / 1000;
if (year >= 1) {
return year + "年" + days_day + "天" + hous_day + "小时" + minutes + "分" + second + "秒";
} else {
if (days >= 1) {
return days + "天" + hous_day + "小时" + minutes + "分" + second + "秒";
} else {
return hours + "小时" + minutes + "分" + second + "秒";
}
}
}
/**
* =============================================== 其他时间工具 ===================================================
*/
/**
* 把Date转化成Calendar
*/
public static Calendar getCalendar(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal;
}
/**
* @return 获取当前时间戳, 精确到秒
*/
public static String timeStemp() {
long currentTimeMillis = System.currentTimeMillis();
String time = String.valueOf(currentTimeMillis / 1000);
return time;
}
/**
* 时间比较
* date1 大于 date2 return 1
* date1 小于 date2 return -1
* date1 等于 date2 return 0;
*
* @param date1
* @param date2
* @return
*/
public static int compare_date(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyyMMdd");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
return dt1.getTime() > dt2.getTime() ? 1 : (dt1.getTime() < dt2.getTime() ? -1 : 0);
} catch (Exception e) {
throw new BusinessException("日期比较出错");
}
}
/**
* 修改时间
* <p>
* 业务范围例如:活动结束时间为某时间的23:59:59秒结束
* <p>
*
* @param date
* @param str 23:59:59 或者 2019-10-30
* @return 修改时分秒 传出数据格式为:2019-10-30 23:59:59
*/
public static String updateTime(Date date, String str) {
if (str.contains(Line)) {
// 修改年月日
String[] timeSplit = getYyyy_Mm_Dd_Hh_Mm_Ss(date).split(" ");
return str + " " + timeSplit[1];
} else if (str.contains(COLON)) {
// 修改时分秒
String[] timeSplit = getYyyy_Mm_Dd_Hh_Mm_Ss(date).split(" ");
return timeSplit[0] + " " + str;
}
return null;
}
/**
* LocalDateTime数据,转时间字符串
*
* @param time LocalDateTime类型的数据
* @param format 时间格式化方式
* @return
*/
public static String localDateTimeConvertString(LocalDateTime time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return df.format(time);
}
/**
* 时间字符串,转换为LocalDateTime类型
*
* @param time 时间字符串 例如:2019-10-30 23:59:59
* @param format 时间格式化方式
* @return
*/
public static LocalDateTime StringConvertLocalDateTime(String time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(time, df);
}
/**
* 时间字符串,转换为LocalDate类型
*
* @param time
* @param format
* @return
*/
public static LocalDate StringConvertLocalDate(String time, String format) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
return LocalDate.parse(time, df);
}
/**
* 获取昨天时间
* <p>
* 调用方法:
* String yesterdayStart = getYesterday(" 00:00:00");
* String yesterdayEnd = getYesterday(" 23:59:59");
*
* @param hhmmss
* @return
*/
public static String getYesterday(String hhmmss) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday = new SimpleDateFormat("yyyy-MM-dd ").format(cal.getTime()) + hhmmss;
return yesterday;
}
/**
* 获取3天前的当前时间
* @return
*/
public static String getLast3Days() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -3);
String yesterday = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
return yesterday;
}
}
有空写个时间工具类
最新推荐文章于 2023-09-13 20:11:40 发布