关于一些日期工具方法
1、常量
主要是一些常用格式还有数据。
public static final long SECOND = 1000L; public static final long MINUTE = 60000L; public static final long HOUR = 3600000L; public static final long DAY = 86400000L; public static final long WEEK = 604800000L; public static final String HOUR_FORMAT = "HH"; public static final String DAY_BEGIN_STRING_HHMMSS = " 00:00:00"; public static final String DAY_END_STRING_HHMMSS = " 23:59:59"; public static final String TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss"; public static final String TIME_FORMAT_FULL_SIMP = "yyyyMMddHHmmss"; public static final String TIME_FORMAT_YMD = "yyyy-MM-dd"; public static final String TIME_FORMAT_YMD_SIMP = "yyyyMMdd";
2、常用方法
1)获取当前时间戳
public static Timestamp getCurrentTimestamp() { return new Timestamp(System.currentTimeMillis()); }
2)获取指定时间的时间戳
public static Timestamp getTimestamp(Date date) { return new Timestamp(date.getTime()); }
3)日期格式转为字符串
public static String dateFormat(Date date, String formatStr) { if(date == null){ return null; } DateFormat simpleDateFormat = new SimpleDateFormat(formatStr); return simpleDateFormat.format(date); }
4)字符串转日期格式
public static Date parse(String str, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); try { return sdf.parse(str); } catch (ParseException var4) { var4.printStackTrace(); return null; } }
5)获取当前年、月、日、周几
/** * 获取指定日期当前年份 * @param date * @return */ public static int getYear(Date date) { Calendar calendar = Calendar.getInstance(); if(date != null){ calendar.setTime(date); } return calendar.get(Calendar.YEAR); } /** * 获取指定日期当前月份 * @param date * @return */ public static int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); if(date != null){ calendar.setTime(date); } return calendar.get(Calendar.MONTH) + 1; } /** * 获取指定日期当前号数 * @param date * @return */ public static int getDay(Date date){ Calendar calendar = Calendar.getInstance(); if(date != null){ calendar.setTime(date); } return calendar.get(Calendar.DATE); } /** * 获取当前周几(0:周日 1:周一 2:周二 3:周三 4:周四 5:周五 6:周六 ) * @return */ public static int getWeek(Date date){ Calendar calendar = Calendar.getInstance(); if(date != null){ calendar.setTime(date); } return calendar.get(Calendar.DAY_OF_WEEK) - 1; }
6)获取指定日期增加年、月、日、时、分、毫秒
基础方法
/** * 时间增加基础方法 * @param date * @param calendarField * @param amount * @return */ private static Date add(Date date, int calendarField, int amount) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(calendarField, amount); return c.getTime(); }
由基础方法扩展出来的方法
/** * 年份增加 * @param date * @param amount * @return */ public static Date addYears(Date date, int amount) { return add(date, Calendar.YEAR, amount); } /** * 月份增加 * @param date * @param amount * @return */ public static Date addMonths(Date date, int amount) { return add(date, Calendar.MONTH, amount); } /** * 周数增加 * @param date * @param amount * @return */ public static Date addWeeks(Date date, int amount) { return add(date, Calendar.WEEK_OF_YEAR, amount); } /** * 天数增加 * @param date * @param amount * @return */ public static Date addDays(Date date, int amount) { return add(date, Calendar.DATE, amount); } /** * 小时数增加 * @param date * @param amount * @return */ public static Date addHours(Date date, int amount) { return add(date, Calendar.HOUR_OF_DAY, amount); } /** * 分钟数增加 * @param date * @param amount * @return */ public static Date addMinutes(Date date, int amount) { return add(date, Calendar.MINUTE, amount); } /** * 秒钟数增加 * @param date * @param amount * @return */ public static Date addSeconds(Date date, int amount) { return add(date, Calendar.SECOND, amount); } /** * 毫秒数增加 * @param date * @param amount * @return */ public static Date addMilliseconds(Date date, int amount) { return add(date, Calendar.MILLISECOND, amount); }
7)设置指定时间的年、月、日、时、分、秒、毫秒
基础方法
/** * 时间数设置基础方法 * @param date * @param calendarField * @param amount * @return */ private static Date set(Date date, int calendarField, int amount) { Calendar c = Calendar.getInstance(); c.setLenient(false); c.setTime(date); c.set(calendarField, amount); return c.getTime(); }
由基础方法扩展的方法
/** * 年份设置 * @param date * @param amount * @return */ public static Date setYears(Date date, int amount) { return set(date, Calendar.YEAR, amount); } /** * 月份设置 * @param date * @param amount (不是实际月份数,0表示1月 1表示2月 .... 11表示12月) * @return */ public static Date setMonths(Date date, int amount) { return set(date, Calendar.MONTH, amount); } /** * 天数设置 * @param date * @param amount * @return */ public static Date setDays(Date date, int amount) { return set(date, Calendar.DATE, amount); } /** * 小时数设置 * @param date * @param amount * @return */ public static Date setHours(Date date, int amount) { return set(date, Calendar.HOUR_OF_DAY, amount); } /** * 分钟数设置 * @param date * @param amount * @return */ public static Date setMinutes(Date date, int amount) { return set(date, Calendar.MINUTE, amount); } /** * 秒数设置 * @param date * @param amount * @return */ public static Date setSeconds(Date date, int amount) { return set(date, Calendar.SECOND, amount); } /** * 毫秒数设置 * @param date * @param amount * @return */ public static Date setMilliseconds(Date date, int amount) { return set(date, Calendar.MILLISECOND, amount); }
8)获取指定日期的开始时间、结束时间
public static Date getDayBegin(Date date) { if (CommonUtil.isNull(date)) { return null; } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = simpleDateFormat.format(date); return newDate(dateStr + " 00:00:00", "yyyy-MM-dd HH:mm:ss"); } public static Date getDayEnd(Date date) { if (CommonUtil.isNull(date)) { return null; } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = simpleDateFormat.format(date); return newDate(dateStr + " 23:59:59", "yyyy-MM-dd HH:mm:ss"); }
9)计算两个日期的各种时间差
/** * 两个日期相减得到的毫秒数 * @param beginDate * @param endDate * @return */ public static long dateDiff(Date beginDate, Date endDate) { long date1ms = beginDate.getTime(); long date2ms = endDate.getTime(); return date2ms - date1ms; } /** * 两个日期相减得到的天数 * @param beginDate * @param endDate * @return */ public static int getDiffDays(Date beginDate, Date endDate) { long days = dateDiff(beginDate, endDate)/DateUtil.DAY; return (int) days; } /** * 两个日期相减得到的小时数 * @param beginDate * @param endDate * @return */ public static int getDiffHours(Date beginDate, Date endDate) { long days = dateDiff(beginDate, endDate)/DateUtil.HOUR; return (int) days; }
至于大家可能会问,为什么没有年月这两个的,主要是每个月天数,每年天数不固定,要做具体需要。
10)各种常见的方法
/** * 获取昨天的开始时间 * @return */ public static Date getBeginDayOfYesterday() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(DAY_OF_MONTH, -1); return cal.getTime(); } /** * 获取昨天的结束时间 * @return */ public static Date getEndDayOfYesterDay() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(DAY_OF_MONTH, -1); return cal.getTime(); }
/** * 获取明天的开始时间 * @return */ public static Date getBeginDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayBegin()); cal.add(DAY_OF_MONTH, 1); return cal.getTime(); } /** * 获取明天的结束时间 * @return */ public static Date getEndDayOfTomorrow() { Calendar cal = new GregorianCalendar(); cal.setTime(getDayEnd()); cal.add(DAY_OF_MONTH, 1); return cal.getTime(); }
/** * 获取本周的开始时间 * @return */ @SuppressWarnings("unused") public static Date getBeginDayOfWeek() { Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return getDayBegin(cal.getTime()); } /** * 获取本周的结束时间 * @return */ public static Date getEndDayOfWeek() { Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek()); cal.add(Calendar.DAY_OF_WEEK, 6); return getDayEnd(cal.getTime()); }
/** * 获取上周的开始时间 * @return */ @SuppressWarnings("unused") public static Date getBeginDayOfLastWeek() { return addWeeks(getBeginDayOfWeek(), -1); } /** * 获取上周的结束时间 * @return */ public static Date getEndDayOfLastWeek() { return addWeeks(getEndDayOfWeek(), -1); }
/** * 获取本月的开始时间 * @return */ public static Date getBeginDayOfMonth() { return getDayBegin(setDays(new Date(), 1)); } /** * 获取本月的结束时间 * @return */ public static Date getEndDayOfMonth() { Calendar calendar = Calendar.getInstance(); int last = calendar.getActualMaximum(Calendar.DATE); return getDayEnd(setDays(calendar.getTime(), last)); }
/** * 获取指定日期当月开始时间 * @return */ public static Date getBeginDayOfMonth(Date date) { return getDayBegin(setDays(date, 1)); } /** * 获取指定日期当月结束时间 * @return */ public static Date getEndDayOfMonth(Date date) { return getDayEnd(addMinutes(getBeginDayOfMonth(addMonths(date, 1)), -1)); }
/** * 获取上月的开始时间 * @return */ public static Date getBeginDayOfLastMonth() { return addMonths(getBeginDayOfMonth(), -1); } /** * 获取上月的结束时间 * @return */ public static Date getEndDayOfLastMonth() { Date date = addDays(getBeginDayOfMonth(), -1); return getDayEnd(date); }
/** * 获取本年的开始时间 * @return */ public static Date getBeginDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayBegin(cal.getTime()); } /** * 获取本年的结束时间 * @return */ public static Date getEndDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEnd(cal.getTime()); }
/** * 获取指定日期的当年开始时间 * @param date * @return */ public static Date getBeginDayOfYear(Date date) { Calendar cal = new GregorianCalendar(); cal.setTime(date); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayBegin(cal.getTime()); } /** * 获取指定日期的当年结束时间 * @param date * @return */ public static Date getEndDayOfYear(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEnd(cal.getTime()); }
如本文有侵权行为,请及时与本人联系,多多包涵!
小生初出茅庐,多多指教!
本文来自博客园,作者:it-小林,转载请注明原文链接:https://www.cnblogs.com/linruitao/p/17532900.html