java 日期格式化工具类
代码如下
package com.dongao.project.utils; import com.ruoyi.common.utils.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import java.lang.management.ManagementFactory; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @Version 1.0 * @Date 2019-08-19 **/ public class DateUtils extends org.apache.commons.lang3.time.DateUtils { public static final String YYYY = "yyyy"; public static final String YYYY_MM = "yyyy-MM"; public static final String YYYY_MM_DD = "yyyy-MM-dd"; public static final String YYYY_MM_DD_HH_MM = "yyyyMMddHHmm"; public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String MM_DD_HH_MM = "MM-dd HH:mm"; 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 getMmTime() { return dateTimeNow(YYYY_MM_DD_HH_MM); } 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); } } /** * 日期路径 即年/月/日 如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 + "分钟"; } public static String getFormatDate(String format,Date date) { if (null != date) { if (StringUtils.isEmpty(format)) { format = YYYY_MM_DD_HH_MM_SS; } String dateStr = parseDateToStr(format, date); return dateStr; } return ""; } /** * 秒转小时分 * @param second * @return */ public static String getFormatTime(Long second) { if (second != null) { if(second < 3600) {//分 return NumFormat(second / 60) + "分"; } if(second< 3600 * 24) {//时 return NumFormat(second/ 60 / 60) + "小时" + NumFormat(second/ 60 % 60) + "分"; } if(second>= 3600 * 24) {//天 return NumFormat(second/ 60 / 60 /24) + "天" +NumFormat(second/ 60 / 60 % 24) + "小时" + NumFormat(second/ 60 % 60) + "分"; } } return "--"; } /** * 格式化时间 * @param sec * @return */ private static String NumFormat(long sec) { if (String.valueOf(sec).length() < 2){ return "0"+sec; }else { return String.valueOf(sec); } } /** * 获取当前时间前面某天或者后面某天 * @param date * @return */ public static Date getOtherDay(Date date, int amount) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH,amount); Date time = calendar.getTime(); return time; } /** * 获取当前时间月日 7.13 * @param date * @return */ public static String getMonthDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); //month从0开始 int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); return month+"."+day; } /** * 获取周几 * @param date * @return */ public static String getWeek(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekday = calendar.get(Calendar.DAY_OF_WEEK); String week = null; if (weekday == 1) { week = "周日"; } else if (weekday == 2) { week = "周一"; } else if (weekday == 3) { week = "周二"; } else if (weekday == 4) { week = "周三"; } else if (weekday == 5) { week = "周四"; } else if (weekday == 6) { week = "周五"; } else if (weekday == 7) { week = "周六"; } return week; } /** * 根据日期字符串获取时间 * @param str * @return */ public static Date getDate(String str) { SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD); Date parse = null; try { parse = sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return parse; } /** * 根据日期字符串获取时间 * @param format * @param str * @return */ public static Date getDate(String format,String str) { if (StringUtils.isEmpty(format)) { format = YYYY_MM_DD_HH_MM_SS; } SimpleDateFormat sdf = new SimpleDateFormat(format); Date parse = null; try { parse = sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return parse; } /** * 格式化日期 * @param format * @param date * @return */ public static Date getChangeDate(String format,Date date) { if (null != date) { if (StringUtils.isEmpty(format)) { format = YYYY_MM_DD_HH_MM_SS; } String dateStr = parseDateToStr(format, date); Date resDate = getDate(format, dateStr); return resDate; } return null; } /** * 计算两个日期相差几天,不计算结束日期当天 * @param start 开始时间 * @param end 结束时间 * @return */ public static long getDiffDays(Date start,Date end) { long dayNum = 0; if (start != null && end != null) { //格式化时间 Date startdate = getChangeDate(YYYY_MM_DD,start); Date enddate = getChangeDate(YYYY_MM_DD,end); //获取时间差 long subtract = enddate.getTime() - startdate.getTime(); //转换为天数 dayNum = subtract / 1000 / 60 / 60 / 24; } return dayNum; } /** * 判断是不是本周 * @param date * @return */ public static boolean isThisWeek(Date date) { Calendar calendar = Calendar.getInstance(); int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR); calendar.setTime(date); int dateWeek = calendar.get(Calendar.WEEK_OF_YEAR); if (currentWeek == dateWeek) { return true; } return false; } /** * 判断是否是当天 * @param date * @return */ public static boolean isThisDay (Date date) { return isThisTime(date,YYYY_MM_DD); } /** * 判断是否是本月 * @param date * @return */ public static boolean isThisMonth (Date date) { return isThisTime(date,YYYY_MM); } /** * 判断当前时间 * @param date * @param format * @return */ private static boolean isThisTime(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); String dateFormat = sdf.format(date); String nowFormat = sdf.format(new Date()); if (dateFormat.equals(nowFormat)) { return true; } return false; } }
注: 可直接使用,如有问题欢迎交流,后续遇到好的会继续更新