java处理日期的工具类DateUtil

这篇文章主要为大家详细介绍了java处理日期的工具类DateUtil,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

java中处理日期的工具类DateUtil,供大家参考,具体内容如下

 package com.leo.demo.othertest; import org.slf4j.LoggerFactory; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.logging.Logger; /** * 时间工具类 */ public final class DateUtil { private static Logger logger = (Logger) LoggerFactory.getLogger(DateUtil.class); /** * 日期格式 */ public interface DATE_PATTERN { String HHMMSS = "HHmmss"; String HH_MM_SS = "HH:mm:ss"; String HH_MM = "HH:mm"; String YYYY = "yyyy"; String YYYYMMDD = "yyyyMMdd"; String YYYYMM = "yyyyMM"; 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"; } /** * 获取当前时间 * * @return Timestamp对象 */ public static Timestamp getCurrontTime() { Timestamp sqlTimestamp = new Timestamp(new Date().getTime()); return sqlTimestamp; } /** * 将Date类型转换成String类型 * * @param date Date对象 * @return 形如:"yyyy-MM-dd HH:mm:ss" */ public static String date2String(Date date) { return date2String(date, DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); } /** * 将Date按格式转化成String * * @param date  Date对象 * @param pattern 日期类型 * @return String */ public static String date2String(Date date, String pattern) { if (date == null || pattern == null) { return null; } return new SimpleDateFormat(pattern).format(date); } /** * 将String类型转换成Date类型 * * @param date Date对象 * @return */ public static Date string2Date(String date) { SimpleDateFormat format = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); try { return format.parse(date); } catch (ParseException e) { return null; } } /** * 获取某日期N天后的日期 * * @param datestr * @param day * @return */ public static Date getBeforeAfterDate(String datestr, int day) { SimpleDateFormat df = new SimpleDateFormat(DATE_PATTERN.YYYY_MM_DD_HH_MM_SS); java.sql.Date olddate = null; try { df.setLenient(false); olddate = new java.sql.Date(df.parse(datestr).getTime()); } catch (ParseException e) { throw new RuntimeException("日期转换错误"); } Calendar cal = new GregorianCalendar(); cal.setTime(olddate); int Year = cal.get(Calendar.YEAR); int Month = cal.get(Calendar.MONTH); int Day = cal.get(Calendar.DAY_OF_MONTH); int NewDay = Day + day; cal.set(Calendar.YEAR, Year); cal.set(Calendar.MONTH, Month); cal.set(Calendar.DAY_OF_MONTH, NewDay); return new Date(cal.getTimeInMillis()); } /** * @return * @Description: 获取当前日期的前一天 * @ReturnType String * @author: liyl * @Created 2015年11月13日 下午5:11:14 */ public static Date currentBeforeDay() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); } /** * @return * @Description: 获取当前日期的后一天 * @ReturnType Date * @author: liyl * @Created 2015年11月13日 下午5:14:54 */ public static Date currentNextDay() { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, 1); return calendar.getTime(); } /** * 获取指定日期星期几(int) * * @param dt * @return */ public static int getWeekOfInt(Date dt) { int[] weekDays = {7, 1, 2, 3, 4, 5, 6}; Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w <0) { w = 0; } return weekDays[w]; } /** * 获取指定日期星期几 * * @param dt * @return */ public static String getWeekOfDate(Date dt) { String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w <0) { w = 0; } return weekDays[w]; } /** * 时间比大小 * * @param DATE1 * @param DATE2 * @param pattern * @return */ public static int compareDate(String DATE1, String DATE2, String pattern) { DateFormat df = new SimpleDateFormat(pattern); try { Date dt1 = df.parse(DATE1); Date dt2 = df.parse(DATE2); if (dt1.getTime() > dt2.getTime()) { System.out.println("dt1 在dt2前"); return 1; } else if (dt1.getTime()  -1) { result = date.replace(".", "-"); } else if (date.indexOf("年") > -1) { result = date.replace("年", "-").replace("月", "-").replace("日", ""); } else if (date.indexOf("-") > -1) { result = date.replace("年", "-").replace("月", "-").replace("日", ""); } else { result = date; } return result; } /** * 获取两个日期相差的月数 * * @param d1 较大的日期 * @param d2 较小的日期 * @return 如果d1>d2返回 月数差 否则返回0 */ public static int monthsBetween(Date d1, Date d2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(d1); c2.setTime(d2); if (c1.getTimeInMillis() d2返回 月数差 否则返回0 */ public static int daysBetween(Date date1, Date date2) { DateFormat sdf = new SimpleDateFormat(DATE_PATTERN.YYYYMMDD); Calendar cal = Calendar.getInstance(); try { Date d1 = sdf.parse(date2String(date1, DATE_PATTERN.YYYYMMDD)); Date d2 = sdf.parse(date2String(date2, DATE_PATTERN.YYYYMMDD)); cal.setTime(d1); long time1 = cal.getTimeInMillis(); cal.setTime(d2); long time2 = cal.getTimeInMillis(); return Integer.parseInt(String.valueOf((time2 - time1) / 86400000L)); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * 计算date2 - date1之间相差的分钟 * * @param date1 * @param date2 * @return */ @SuppressWarnings("deprecation") public static int minutesBetween(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); // date1.setSeconds(0); cal.setTime(date1); long time1 = cal.getTimeInMillis(); cal.setTime(date2); long time2 = cal.getTimeInMillis(); if (time2 - time1 <= 0) { return 0; } else { return Integer.parseInt(String.valueOf((time2 - time1) / 60000L)) + 1; } } /** * 计算date2 - date1之间相差的秒 * * @param date1 * @param date2 * @return */ @SuppressWarnings("deprecation") public static int secondBetween(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); // date1.setSeconds(0); cal.setTime(date1); long time1 = cal.getTimeInMillis(); cal.setTime(date2); long time2 = cal.getTimeInMillis(); if (time2 - time1 <= 0) { return 0; } else { return Integer.parseInt(String.valueOf((time2 - time1) / 1000L)) + 1; } } /** * 计算date2 - date1之间相差的毫秒 * * @param date1 * @param date2 * @return */ @SuppressWarnings("deprecation") public static int millisecondBetween(Date date1, Date date2) { Calendar cal = Calendar.getInstance(); cal.setTime(date1); long time1 = cal.getTimeInMillis(); cal.setTime(date2); long time2 = cal.getTimeInMillis(); if (time2 - time1 <= 0) { return 0; } else { return Integer.parseInt(String.valueOf((time2 - time1))); } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是java处理日期的工具类DateUtil的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java