package com.jd.rc.custom.oper.bff.common; import com.jd.fastbe.framework.client.support.exception.BusinessException; import com.jd.fastbe.framework.client.support.exception.DefaultErrorCodeEnum; import org.apache.commons.lang3.StringUtils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 时间转换工具 * * @author jialong12 * @date 2021-10-14 21:57 */ public class DateUtils { public static final String MM_dd = "MM-dd"; public static final String HH_mm = "HH:mm"; public static final String MM_dd2 = "MM/dd"; private static final String yyyy_MM_dd = "yyyy-MM-dd"; public static final String HH = "HH"; public static final String YYYY_MM_DD = "yyyy-MM-dd"; private static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"; private static final String yyyy_MM_dd_HH = "yyyy-MM-dd HH"; /** * 一天的毫秒 */ private static final long DAY_MILLS = 1000 * 24 * 3600; /** * 解析字符串时间为Date类型 * * @param dateStr 时间 * @return 时间 */ public static Date parse(String dateStr, String pattern) { if (StringUtils.isBlank(dateStr)) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { return sdf.parse(dateStr); } catch (Exception e) { throw new BusinessException(DefaultErrorCodeEnum.BAD_ARGUMENT_ERROR, "时间格式有误", e); } } /** * 转换时间为字符串格式 * * @param date 时间 * @return 时间 */ public static String format(Date date, String pattern) { if (date == null) { return null; } return new SimpleDateFormat(pattern).format(date); } public static Date parseDate(String dateStr) { return parse(dateStr, yyyy_MM_dd); } public static String formatDate(Date date) { return format(date, yyyy_MM_dd); } public static String formatDateTime(Date date) { return format(date, yyyy_MM_dd_HH_mm_ss); } public static String formatDateHH(Date date) { return format(date, yyyy_MM_dd_HH); } public static Date parseDateTime(String dateStr) { return parse(dateStr, yyyy_MM_dd_HH_mm_ss); } public static Date parseDateHH(String dateStr) { return parse(dateStr, yyyy_MM_dd_HH); } public static Date parseHH(String dateStr) { return parse(dateStr, HH); } /** * 结束时间大于等于开始时间 */ public static boolean dateGt(Date after, Date before) { return after.getTime() >= before.getTime(); } /** * 时间设置为 23:59:59 */ public static Date setEndTime(Date date) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } /** * 时间设置为 00:00:00 */ public static Date setStartTime(Date date) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 时间后 * * @param date 当前时间 * @param days 如果为正数,则是多少天后,负数则为多少天前 * @return 日期 */ public static Date after(Date date, int days) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, days); return calendar.getTime(); } /** * 计算相差天数 * * @param before 之前时间 * @param after 之后时间 * @return 天数 */ public static int days(Date before, Date after) { // 天数 return ((int) ((after.getTime() - setStartTime(before).getTime()) / DAY_MILLS)) + 1; } public static void main(String[] args) { Date date = DateUtils.parseDate("2022-07-30"); System.out.println(formatDate(after(date, -1))); System.out.println(formatDate(after(date, -1))); System.out.println(formatDate(after(date, -1))); System.out.println(formatDate(after(date, -1))); } //获取前、后month个月时间 public static Date getDateBefore3Month(Date dNow, Integer month) { if (dNow == null) { return null; } Calendar calendar = Calendar.getInstance(); //得到日历 calendar.setTime(dNow);//把当前时间赋给日历 calendar.add(Calendar.MONTH, month); //设置为前3月 return calendar.getTime(); //得到前3月的时间 } }
标签:return,Date,日期,static,calendar,date,java,工具,public From: https://www.cnblogs.com/niun/p/17117299.html