CalendarHelper日期计算工具,各种日期的获取和计算
Posted Matrix_Ran
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CalendarHelper日期计算工具,各种日期的获取和计算相关的知识,希望对你有一定的参考价值。
今天分享一个日期获取和计算的工具类,这个最初是用在项目中获取每周每月日期用的。
package com.ran.interview; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; /** * @author BMR * @ClassName: CalendarHelper * @Description: 日期计算工具,标准格式yyyy-MM-dd下各种日期的获取和计算 * @date 2016/2/26 16:08 */ public class CalendarHelper { private static SimpleDateFormat yyyyMMddFormat; public final static String[] mMonthCN = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"}; public static void setup() { yyyyMMddFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); } /** * 显示格式化的月日:eg,2013-03-27,convert to 03月27日 * * @param dateStr eg,2013-03-27 * @return 03月27日 */ public static String formatToDay(String dateStr) { if (CalendarHelper.isStrEmpty(dateStr)) { return ""; } //DateFormat localFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA); DateFormat localFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINESE); Date date; String formatDate = null; try { date = getDateFromString(dateStr, null); formatDate = localFormat.format(date); formatDate = formatDate.substring(5); } catch (ParseException e) { e.printStackTrace(); } return formatDate; } /** * 显示格式化的月,中文的月份 * * @param dateStr eg,2013-03-27 * @return 三月 */ public static String formatToMonth(String dateStr) { if (CalendarHelper.isStrEmpty(dateStr)) { return ""; } if(dateStr.length() < 7){ return ""; } String month = dateStr.substring(5, 7); int index = Integer.parseInt(month); String formatDate = ""; if (index > 0 && index < 13) { formatDate = mMonthCN[index - 1]; } return formatDate; } /** * 检查字符串是否是今天 * * @param dateStr */ public static boolean checkIsToday(String dateStr) { Calendar calendar = Calendar.getInstance(); Date currentDate = calendar.getTime(); return getStringFromDate(currentDate, null).equals(dateStr); } /** * Get the Date from String with custom format. Default format is yyyy-MM-dd * * @param dateString * @param dateFormat * @return * @throws ParseException */ public static Date getDateFromString(String dateString, String dateFormat) throws ParseException { SimpleDateFormat formatter; if (dateFormat == null) { if (yyyyMMddFormat == null) { setup(); } formatter = yyyyMMddFormat; } else { formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH); } return formatter.parse(dateString); } /** * Convert the Date to String with custom format. Default format is yyyy-MM-dd * * @param date * @param dateFormat * @return */ public static String getStringFromDate(Date date, String dateFormat) { String dateString = null; SimpleDateFormat formatter; if (date == null) { return null; } if (dateFormat == null) { if (yyyyMMddFormat == null) { setup(); } formatter = yyyyMMddFormat; } else { formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH); } dateString = formatter.format(date); return dateString; } /** * 获取当天的日期 * * @return */ public static String getTodayDateString() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); return dateFormat.format(new Date()); } /** * 获取指定日期的偏移后的日期字符串,offset>0日期向后,<0向前 * * @param dateStr * @param offset * @return */ public static String getDateStringByOffset(String dateStr, int offset) { if (offset == 0) { return dateStr; } Date date; String newDateStr = null; try { date = getDateFromString(dateStr, null); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, offset); newDateStr = getStringFromDate(cal.getTime(), null); } catch (ParseException e) { e.printStackTrace(); } return newDateStr; } /** * 获取输入日期所在周的开始日期 (周一),默认格式 yyyy-MM-dd * * @param date * @return String */ public static String getFirstDayOfWeek(Date date, String dateFormat) { String firstDay; if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); if (calendar.get(Calendar.DAY_OF_WEEK) == 1) { calendar.add(Calendar.DATE, -7); } calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); Date newDate = calendar.getTime(); firstDay = getStringFromDate(newDate, dateFormat); return firstDay; } /** * 获取输入日期所在周的结束日期 (周日),默认格式 yyyy-MM-dd * * @param date * @return String */ public static String getLastDayOfWeek(Date date, String dateFormat) { String lastDay; if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 默认周日是本周的开始, if (calendar.get(Calendar.DAY_OF_WEEK) != 1) { // 非周末的情况要先加一周才能获得本周的周末 calendar.add(Calendar.DATE, 7); } calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); Date newDate = calendar.getTime(); lastDay = getStringFromDate(newDate, dateFormat); return lastDay; } /** * 获得指定日期与指定周数间隔的那周周一的日期 * * @param date 正数是count周数之后,负数时count周数之前,0 返回本周周一日期 * @param dateFormat * @return */ public static String getFirstDayOfPreviousWeek(Date date, int count, String dateFormat) { String firstDay; if (date == null) { return null; } if (count == 0) { return getFirstDayOfWeek(date, dateFormat); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 默认周日是本周的开始,如果是周末,不加 if (calendar.get(Calendar.DAY_OF_WEEK) == 1) { calendar.add(Calendar.DATE, (count - 1) * 7); } else { calendar.add(Calendar.DATE, count * 7); } calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); Date newDate = calendar.getTime(); firstDay = getStringFromDate(newDate, dateFormat); return firstDay; } /** * 获得指定日期与指定周数间隔的那周周日的日期 * * @param date 正数是count周数之后,负数时count周数之前,0 返回本周周日日期 * @param dateFormat * @return */ public static String getLastDayOfPreviousWeek(Date date, int count, String dateFormat) { String lastDay; if (date == null) { return null; } if (count == 0) { return getLastDayOfWeek(date, dateFormat); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 默认周日是本周的开始,要先加一周才能获得本周的周末。如果是周末,不加 if (calendar.get(Calendar.DAY_OF_WEEK) == 1) { calendar.add(Calendar.DATE, (count) * 7); } else { // 非周末的情况要先加一周才能获得本周的周末 calendar.add(Calendar.DATE, (count + 1) * 7); } calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); Date newDate = calendar.getTime(); lastDay = getStringFromDate(newDate, dateFormat); return lastDay; } /** * 获取输入日期所在月的开始日期,默认格式 yyyy-MM-dd * * @param date * @return */ public static String getFirstDayOfMonth(Date date, String dateFormat) { String firstDay; if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DATE, 1); Date newDate = calendar.getTime(); firstDay = getStringFromDate(newDate, dateFormat); return firstDay; } /** * 获取输入日期所在月的结束日期,默认格式 yyyy-MM-dd * * @param date * @return String */ public static String getLastDayOfMonth(Date date, String dateFormat) { String lastDay; if (date == null) { return null; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); Date newDate = calendar.getTime(); lastDay = getStringFromDate(newDate, dateFormat); return lastDay; } /** * 获取输入日期和间隔月份所在月的开始日期,默认格式 yyyy-MM-dd * * @param date * @param count * @return */ public static String getFirstDayOfPreviousMonth(Date date, int count, String dateFormat) { String firstDay; if (date == null) { return null; } if (count == 0) { return getFirstDayOfMonth(date, dateFormat); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, count); calendar.set(Calendar.DATE, 1); Date newDate = calendar.getTime(); firstDay = getStringFromDate(newDate, dateFormat); return firstDay; } /** * 获取输入日期和间隔月份所在月的最后日期,默认格式 yyyy-MM-dd * * @param date * @param count * @return */ public static String getLastDayOfPreviousMonth(Date date, int count, String dateFormat) { String firstDay; if (date == null) { return null; } if (count == 0) { return getLastDayOfMonth(date, dateFormat); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, count); calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); Date newDate = calendar.getTime(); firstDay = getStringFromDate(newDate, dateFormat); return firstDay; } /** * 计算两个日期字符串之间的天数,格式2011-12-26 * * @param dateStart 起始日期 * @param dateEnd 结束日期 * @return */ public static int getIntervalDay(String dateStart, String dateEnd) { Date dateFrom; Date dateTo; long gap = 0; try { dateFrom = getDateFromString(dateStart, null); dateTo = getDateFromString(dateEnd, null); Calendar cal = Calendar.getInstance(); cal.setTime(dateFrom); long timeFrom = cal.getTimeInMillis(); cal.setTime(dateTo); long timeTo = cal.getTimeInMillis(); gap = (timeTo - timeFrom) / (1000 * 3600 * 24); gap = gap + 1; //从间隔毫秒变成间隔天数</strong> } catch (ParseException e) { e.printStackTrace(); } return (int)gap; } /** * 判断字符串是否为空 * * @param str * @return */ public static boolean isStrEmpty(String str) { if (null == str || "".equals(str.trim()) || "null".equals(str)) { return true; } return false; } }
最后附上几个简单的测试用例:
package com.ran.interview; import java.util.Date; /** * @ClassName: CalendarHelperTest * @Description: TODO * @author BMR * @date 2016年3月15日 上午11:00:49 */ public class CalendarHelperTest { public static void main(String[] args){ String todayStr = CalendarHelper.getTodayDateString(); System.out.println("today is: "+ todayStr); System.out.println("check is today: "+ CalendarHelper.checkIsToday(todayStr)); System.out.println("today is (Chinese format): "+ CalendarHelper.formatToDay(todayStr)); System.out.println("今天所在周的周一: "+ CalendarHelper.getFirstDayOfWeek(new Date(), null)); System.out.println("今天所在周的周末: "+ CalendarHelper.getLastDayOfWeek(new Date(), null)); System.out.println("指定日期的格式化的月,中文的月份: "+ CalendarHelper.formatToMonth(todayStr)); System.out.println("获取输入日期所在月的开始日期: "+ CalendarHelper.getFirstDayOfMonth(new Date(), null)); System.out.println("获取输入日期所在月的结束日期: "+ CalendarHelper.getLastDayOfMonth(new Date(), null)); System.out.println("获得指定日期与指定周数间隔的那周周一的日期: "+ CalendarHelper.getFirstDayOfPreviousWeek(new Date(), 2, null)); System.out.println("获得指定日期与指定周数间隔的那周周末的日期: "+ CalendarHelper.getLastDayOfPreviousWeek(new Date(), -2, null)); System.out.println("获取输入日期和间隔月份所在月的开始日期: "+ CalendarHelper.getFirstDayOfPreviousMonth(new Date(), 2, null)); System.out.println("获取输入日期和间隔月份所在月的最后日期: "+ CalendarHelper.getLastDayOfPreviousMonth(new Date(), -2, null)); System.out.println("计算两个日期字符串之间的天数: "+ CalendarHelper.getIntervalDay("2016-03-01", todayStr)); } }
测试结果打印为:
today is: 2016-03-15 check is today: true today is (Chinese format): 3月15日 今天所在周的周一: 2016-03-14 今天所在周的周末: 2016-03-20 指定日期的格式化的月,中文的月份: 三月 获取输入日期所在月的开始日期: 2016-03-01 获取输入日期所在月的结束日期: 2016-03-31 获得指定日期与指定周数间隔的那周周一的日期: 2016-03-28 获得指定日期与指定周数间隔的那周周末的日期: 2016-03-06 获取输入日期和间隔月份所在月的开始日期: 2016-05-01 获取输入日期和间隔月份所在月的最后日期: 2016-01-31 计算两个日期字符串之间的天数: 15
以上是关于CalendarHelper日期计算工具,各种日期的获取和计算的主要内容,如果未能解决你的问题,请参考以下文章
7hutool实战:DateUtil(时间工具类)-日期计算
3hutool源码分析:DateUtil(时间工具类)-获取日期的各种内容
3hutool源码分析:DateUtil(时间工具类)-获取日期的各种内容