日期操作utils
Posted wangquanyi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日期操作utils相关的知识,希望对你有一定的参考价值。
日期操作utils
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * 日期操作辅助类 * **/ public class DateUtil { private DateUtil() { } /** 日期格式 **/ public interface DATE_PATTERN { String HHMMSS = "HHmmss"; String HH_MM_SS = "HH:mm:ss"; String YYYYMMDD = "yyyyMMdd"; String YYYY_MM_DD = "yyyy-MM-dd"; String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS"; String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; String MMDD = "MM月dd日"; } /** * 格式化日期 * * @param date * @return */ public static final String format(Object date) { return format(date, DATE_PATTERN.YYYY_MM_DD); } /** * 格式化日期 * * @param date * @param pattern * @return */ public static final String format(Object date, String pattern) { if (date == null) { return null; } if (pattern == null) { return format(date); } return new SimpleDateFormat(pattern).format(date); } /** * 获取日期 * * @return */ public static final String getDate() { return format(new Date()); } /** * 获取日期时间 * * @return */ public static final String getDateTime() { return format(new Date(), DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * 获取日期时间 * * @return */ public static final String getDateTimeSs() { return format(new Date(), DATE_PATTERN.YYYYMMDDHHMMSSSSS); } /** * 获取日期 * * @param pattern * @return */ public static final String getDateTime(String pattern) { return format(new Date(), pattern); } /** * 日期计算 * * @param date * @param field * @param amount * @return */ public static final Date addDate(Date date, int field, int amount) { if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(field, amount); return calendar.getTime(); } /** * 字符串转换为日期:不支持yyM[M]d[d]格式 * * @param date * @return */ public static final Date stringToDate(String date) { if (date == null) { return null; } String separator = String.valueOf(date.charAt(4)); String pattern = "yyyyMMdd"; if (!separator.matches("\d*")) { pattern = "yyyy" + separator + "MM" + separator + "dd"; if (date.length() < 10) { pattern = "yyyy" + separator + "M" + separator + "d"; } } else if (date.length() < 8) { pattern = "yyyyMd"; } pattern += " HH:mm:ss.SSS"; pattern = pattern.substring(0, Math.min(pattern.length(), date.length())); try { return new SimpleDateFormat(pattern).parse(date); } catch (ParseException e) { return null; } } /** * 间隔天数 * * @param startDate * @param endDate * @return */ public static final Integer getDayBetween(Date startDate, Date endDate) { Calendar start = Calendar.getInstance(); start.setTime(startDate); start.set(Calendar.HOUR_OF_DAY, 0); start.set(Calendar.MINUTE, 0); start.set(Calendar.SECOND, 0); start.set(Calendar.MILLISECOND, 0); Calendar end = Calendar.getInstance(); end.setTime(endDate); end.set(Calendar.HOUR_OF_DAY, 0); end.set(Calendar.MINUTE, 0); end.set(Calendar.SECOND, 0); end.set(Calendar.MILLISECOND, 0); long n = end.getTimeInMillis() - start.getTimeInMillis(); return (int) (n / (60 * 60 * 24 * 1000L)); } /** * 间隔月 * * @param startDate * @param endDate * @return */ public static final Integer getMonthBetween(Date startDate, Date endDate) { if (startDate == null || endDate == null || !startDate.before(endDate)) { return null; } Calendar start = Calendar.getInstance(); start.setTime(startDate); Calendar end = Calendar.getInstance(); end.setTime(endDate); int year1 = start.get(Calendar.YEAR); int year2 = end.get(Calendar.YEAR); int month1 = start.get(Calendar.MONTH); int month2 = end.get(Calendar.MONTH); int n = (year2 - year1) * 12; n = n + month2 - month1; return n; } /** * 间隔月,多一天就多算一个月 * * @param startDate * @param endDate * @return */ public static final Integer getMonthBetweenWithDay(Date startDate, Date endDate) { if (startDate == null || endDate == null || !startDate.before(endDate)) { return null; } Calendar start = Calendar.getInstance(); start.setTime(startDate); Calendar end = Calendar.getInstance(); end.setTime(endDate); int year1 = start.get(Calendar.YEAR); int year2 = end.get(Calendar.YEAR); int month1 = start.get(Calendar.MONTH); int month2 = end.get(Calendar.MONTH); int n = (year2 - year1) * 12; n = n + month2 - month1; int day1 = start.get(Calendar.DAY_OF_MONTH); int day2 = end.get(Calendar.DAY_OF_MONTH); if (day1 <= day2) { n++; } return n; } /** * date转成字符串 * * @param date * @param format * @return */ public static String dateToString(Date date, String format) { String mDate = ""; SimpleDateFormat sdf = new SimpleDateFormat(format); try { mDate = sdf.format(date); } catch (Exception e) { e.printStackTrace(); } return mDate; } /** * 日期转换成日期 * @param date * @param format * @return */ public static Date dateToDate(Date date, String format) { Date tDate = null; String mDate = ""; try { SimpleDateFormat sdf = new SimpleDateFormat(format); mDate = sdf.format(date); tDate = sdf.parse(mDate); } catch (Exception e) { e.printStackTrace(); } return tDate; } /** * 取得当前月份总的天数 * */ public static int getCurrentMonthLastDay() { Calendar a = Calendar.getInstance(); a.set(Calendar.DATE, 1);// 把日期设置为当月第一天 a.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天 int maxDate = a.get(Calendar.DATE); return maxDate; } /** * 得到上个月第一天时间 * * @return Zf 2018年4月2日 上午11:42:47 */ public static Date getLastMonthFirstDay() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); 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(); } /** * 得到上个月最后一天时间 * */ public static Date getLastMonthEndDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, -1); return calendar.getTime(); } /** * 获取这个月第一天时间 * */ public static Date getThisMonthFirstDay() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 1); 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(); } /** * yyyy-MM-dd HH:mm:ss格式的字符串转时间 * * @return */ public static final Date getStringtoDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); try { return sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); return null; } } /** * 时间转成yyyy-MM-dd HH:mm:ss格式的字符串 * * @param date * @return */ public static final String getDatetoString(Date date) { return dateToString(date, DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * 日期比较 * * @param dt1 * @param dt2 * @return 1:dt1 在dt2前 -1:dt1在dt2后 0:dt1等于dt2 */ public static int compare_date(Date dt1, Date dt2 ) { try { if (dt1.getTime() > dt2.getTime()) { return 1; } else if (dt1.getTime() < dt2.getTime()) { return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; } /** * 判断时间是否在时间段内 * @param date 需要判断的时间 * @param begin 开始时间 * @param end 结束时间 * @return */ public static boolean isInDate(Date date, Date begin, Date end) { return (date.getTime()>=begin.getTime())&&(date.getTime()<=end.getTime()); } /** * * 获取当天0点时间或者24点时间 * @param month 0,24 * @return int **/ public static String getTimeSmorningOrSnight(int month){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, month); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return format(cal.getTime(),DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * * 获取当天0点时间或者24点时间 * @param month 0,24 * @return int */ public static Date getTimeSmorningOrSnightDate(int month){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, month); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); return getStringtoDate(format(cal.getTime(),DATE_PATTERN.YYYY_MM_DD_HH_MM_SS)); } /** * * 获取指定日期前几个小时的时间 * @param hour * @return String */ public static String beforeOneHourToNowDate(Date tDate,int hour) { Calendar calendar = Calendar.getInstance(); calendar.setTime(tDate); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - hour); return format(calendar.getTime(),DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * * 获取指定日期后几个小时的时间 * @param hour * @return String */ public static String afterOneHourToNowDate(Date tDate,int hour) { Calendar calendar = Calendar.getInstance(); calendar.setTime(tDate); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour); return format(calendar.getTime(),DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * 根据录入日期为标准,返回为时间增加(减少)相应秒数后的时间 * @param date 录入日期 * @param nSeconds 增加(减少)秒数 * @return 增加(减少相应)秒数后的日期 */ public static String getDateBySeconds(Date date,int nSeconds) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + nSeconds); return format(calendar.getTime(),DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * 以当前时间为标准,得到指定的时间戳 * @param orderDay 指定的天数,可以为负数代表前几天 * @return */ public static Long getOrderDateTime(int orderDay){ return getOrderDateTime(orderDay,new Date()).getTime(); } /** * 取得与原日期相差一定天数的日期,返回Date型日期 * * @param date * 原日期 * @param intBetween * 相差的天数 * @return date加上intBetween天后的日期 */ public static Date getDateBetween(Date date, int intBetween) { return getOrderDateTime(intBetween,date); } /** * 以当前时间为标准,得到指定的时间戳 * @param orderDay 指定的天数,可以为负数代表前几天 * @param orderDate 指定初始时间 * @return */ public static Date getOrderDateTime(int orderDay,Date orderDate){ Calendar calendar = Calendar.getInstance(); calendar.setTime(orderDate); calendar.add(Calendar.DAY_OF_MONTH, orderDay); return calendar.getTime(); } /** * 时间处理 要获取前后几分钟的时间 * @date:2018年4月18日上午11:33:22 * @param date * @return */ public static Date getWhatMin(Date date,int min) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE,min); date = calendar.getTime(); return date; } /** * 上个月的今天 * * @param format * @return */ public static String getLastMonthToDay(String format) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); if (null == format || "".equals(format)) { format = DATE_PATTERN.YYYY_MM_DD; } return dateToString(calendar.getTime(), format); } /** * * 范围指定时间 前几天 某个小时的时间 * @param date 指定时间 * @param day 往前推几天 * @param month 小时 0-24 * @return Date */ public static String returnPre(Date date, int day,int month) { Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, month); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DATE, day); return format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss"); } public static void main(String[] args) { /*Date date = new Date(); System.out.println(format(DateUtil.getOrderDateTime(-7, date), "yyyy-MM-dd HH:mm:ss")); System.out.println(returnPre(new Date(),-6,0)); System.out.println(format(stringToDate(returnPre(new Date(),-6,0)), "MM-dd")); for (int i = -30; i <= 0; i=i+2) { System.out.println(returnPre(new Date(),i,0)); }*/ String date = "1574837075000"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String string = sdf.format(Long.valueOf(date)); System.out.println(string); } /** * 时间类型相互转换 * @param date * @param oldFormat * @param newFormat * @param dayNumber * @return */ public static String dateFomat_String(String date,String oldFormat,String newFormat,int dayNumber){ String resultDataString = ""; try { SimpleDateFormat sdf = new SimpleDateFormat(oldFormat); SimpleDateFormat sdfNew = new SimpleDateFormat(newFormat); Calendar theCa = Calendar.getInstance(); Date startDate = null; if (date != null && !"".equals(date)){ startDate = sdf.parse(date); }else { return "传入时间String不能为null"; } if (dayNumber>0){ theCa.setTime(startDate); theCa.add(theCa.DATE, -dayNumber);//dayNumber:往前多少天 Date start = theCa.getTime(); resultDataString = sdfNew.format(start); }else { resultDataString = sdfNew.format(startDate); } }catch (Exception e){ e.printStackTrace(); } return resultDataString; } /** * 时间类型相互转换 * @param date * @param format * @param dayNumber * @return */ public static String dateFomat_Date(Date date,String format,int dayNumber){ String resultDataString = ""; try { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar theCa = Calendar.getInstance(); Date startDate = null; if (date != null){ if (dayNumber>0){ theCa.setTime(startDate); theCa.add(theCa.DATE, -dayNumber);//dayNumber:往前多少天 Date start = theCa.getTime(); resultDataString = sdf.format(start); }else { resultDataString = sdf.format(startDate); } }else { return "传入时间Date不能为null"; } }catch (Exception e){ e.printStackTrace(); } return resultDataString; } /** * 时间转换为长整型 * @param date * @param format * @return */ public static Long dateFomat_Long(String date,String format){ try { SimpleDateFormat sf = new SimpleDateFormat(format); return sf.parse(date).getTime(); }catch (Exception e){ e.printStackTrace(); } return 0L; } /** * 把yyyy-MM-dd HH:mm:ss格式的字符串 转 ios8601格式yyyy-MM-dd‘T‘HH:mm:ss.SSSzzz格式的字符串 * @param date * @return */ public static String strDateToAsISO(String date){ Date stringtoDate = getStringtoDate(date); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSSzzz"); String s = sdf.format(stringtoDate); return s; } }
以上是关于日期操作utils的主要内容,如果未能解决你的问题,请参考以下文章
sql 日期转换代码片段 - Dato,120,konvertere