Java中封装的全局日期处理工具类
場景
全局日期處理類。
時間日期操作類,集成了大部分時間的應用類。
主要功能如下:
獲取SimpleDateFormat
獲取日期中的某數值。如獲取月份
增加日期中某類型的某數值。如增加日期
獲取精確的日期
判斷字符串是否為日期字符串
獲取日期字符串的日期風格。失敗返回null。
將日期字符串轉化為日期。失敗返回null。
將日期字符串轉化為另一日期字符串。失敗返回null。
增加日期的年份。失敗返回null。
增加日期的月份。失敗返回null。
增加日期的天數。失敗返回null。
增加日期的小時。失敗返回null。
?增加日期的分鐘。失敗返回null。
?增加日期的秒鐘。失敗返回null。
獲取日期的年份。失敗返回0。
獲取日期的月份。失敗返回0。
獲取日期的天數。失敗返回0。
獲取日期的小時。失敗返回0。
獲取日期的分鐘。失敗返回0。
獲取日期的秒鐘。失敗返回0。
獲取日期。默認yyyy-MM-dd格式。失敗返回null。
獲取日期的時間。默認HH:mm:ss格式。失敗返回null。
獲取日期的星期。失敗返回null。
獲取兩個日期相差的天數 。
獲取兩個日期相差的毫秒數 。
獲得兩個日期之間的連續日期.。
將時間轉換為時間戳。
獲取連續月份。
兩個時間相差距離中文描述 。
比較兩個時間的大小。
工具類下載:
https://download.csdn.net/download/badao_liumang_qizhi/11140789
實現
import java.text.DateFormat; import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.time.Month; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;import org.springframework.core.convert.converter.Converter;/*** 全局日期處理類* Convert<T,S>* 泛型T:代表客戶端提交的參數 String* 泛型S:通過convert轉換的類型* 時間日期操作類,集成了大部分時間的應用類,盡力做到最大的復用* @author 霸道流氓氣質* @DateTime */ public class DateConvert implements Converter<String, Date> {@SuppressWarnings("deprecation")public Date convert(String stringDate) {if (stringDate == null || "".equals(stringDate.trim())) {return null;}Date d = DateConvert.formatStringToDate(stringDate);return d;}private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(); private static final Object object = new Object(); /** * 獲取SimpleDateFormat * @param pattern 日期格式 * @return SimpleDateFormat對象 * @throws RuntimeException 異常:非法日期格式 * @description SimpleDateFormat 是一個以國別敏感的方式格式化和分析數據的具體類。* 它允許格式化 (date -> text)、語法分析 (text -> date)和標準化。*/ private static SimpleDateFormat getDateFormat(String pattern) throws RuntimeException { SimpleDateFormat dateFormat = threadLocal.get(); if (dateFormat == null) { synchronized (object) { if (dateFormat == null) { dateFormat = new SimpleDateFormat(pattern); dateFormat.setLenient(false); threadLocal.set(dateFormat); } } } dateFormat.applyPattern(pattern); return dateFormat; } /** * 獲取日期中的某數值。如獲取月份 * @param date 日期 * @param dateType 日期格式 * @return 數值 */ private static int getInteger(Date date, int dateType) { int num = 0; Calendar calendar = Calendar.getInstance(); if (date != null) { calendar.setTime(date); num = calendar.get(dateType); } return num; } /** * 增加日期中某類型的某數值。如增加日期 * @param date 日期字符串 * @param dateType 類型 * @param amount 數值 * @return 計算后日期字符串 */ private static String addInteger(String date, int dateType, int amount) { String dateString = null; DateStyle dateStyle = getDateStyle(date); if (dateStyle != null) { Date myDate = formatStringToDate(date, dateStyle); myDate = addInteger(myDate, dateType, amount); dateString = formatDateToString(myDate, dateStyle); } return dateString; } /** * 增加日期中某類型的某數值。如增加日期 * @param date 日期 * @param dateType 類型 * @param amount 數值 * @return 計算后日期 */ private static Date addInteger(Date date, int dateType, int amount) { Date myDate = null; if (date != null) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(dateType, amount); myDate = calendar.getTime(); } return myDate; } /** * 獲取精確的日期 * @param timestamps 時間long集合 * @return 日期 */ private static Date getAccurateDate(List<Long> timestamps) { Date date = null; long timestamp = 0; Map<Long, long[]> map = new HashMap<Long, long[]>(); List<Long> absoluteValues = new ArrayList<Long>(); if (timestamps != null && timestamps.size() > 0) { if (timestamps.size() > 1) { for (int i = 0; i < timestamps.size(); i++) { for (int j = i + 1; j < timestamps.size(); j++) { long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j)); absoluteValues.add(absoluteValue); long[] timestampTmp = { timestamps.get(i), timestamps.get(j) }; map.put(absoluteValue, timestampTmp); } } // 有可能有相等的情況。如2012-11和2012-11-01。時間戳是相等的。此時minAbsoluteValue為0 // 因此不能將minAbsoluteValue取默認值0 long minAbsoluteValue = -1; if (!absoluteValues.isEmpty()) { minAbsoluteValue = absoluteValues.get(0); for (int i = 1; i < absoluteValues.size(); i++) { if (minAbsoluteValue > absoluteValues.get(i)) { minAbsoluteValue = absoluteValues.get(i); } } } if (minAbsoluteValue != -1) { long[] timestampsLastTmp = map.get(minAbsoluteValue); long dateOne = timestampsLastTmp[0]; long dateTwo = timestampsLastTmp[1]; if (absoluteValues.size() > 1) { timestamp = Math.abs(dateOne) > Math.abs(dateTwo) ? dateOne : dateTwo; } } } else { timestamp = timestamps.get(0); } } if (timestamp != 0) { date = new Date(timestamp); } return date; } /** * 判斷字符串是否為日期字符串 * @param date 日期字符串 * @return true or false */ public static boolean isDate(String date) { boolean isDate = false; if (date != null) { if (getDateStyle(date) != null) { isDate = true; } } return isDate; } /** * 獲取日期字符串的日期風格。失敗返回null。 * @param date 日期字符串 * @return 日期風格 */ public static DateStyle getDateStyle(String date) { DateStyle dateStyle = null; Map<Long, DateStyle> map = new HashMap<Long, DateStyle>(); List<Long> timestamps = new ArrayList<Long>(); for (DateStyle style : DateStyle.values()) { if (style.isShowOnly()) { continue; } Date dateTmp = null; if (date != null) { try { date = style.getValue().equals("yyyy-MM-dd'T'HH:mm:ss.SSS Z")?date.replace("Z", " UTC"):date;ParsePosition pos = new ParsePosition(0); dateTmp = getDateFormat(style.getValue()).parse(date, pos); int dateLen = date.length();boolean dateValide = pos.getIndex() != dateLen;if(dateValide) dateTmp = null;} catch (Exception e) { } } if (dateTmp != null) { timestamps.add(dateTmp.getTime()); map.put(dateTmp.getTime(), style); break;} } Date accurateDate = getAccurateDate(timestamps); if (accurateDate != null) { dateStyle = map.get(accurateDate.getTime()); } return dateStyle; } /** * 將日期字符串轉化為日期。失敗返回null。 * @param date 日期字符串 * @return 日期 */ public static Date formatStringToDate(String date) { DateStyle dateStyle = getDateStyle(date); return formatStringToDate(date, dateStyle); } /** * 將日期字符串轉化為日期。失敗返回null。 * @param date 日期字符串 * @param pattern 日期格式 * @return 日期 */ public static Date formatStringToDate(String date, String pattern) { Date myDate = null; if (date != null) { try { myDate = getDateFormat(pattern).parse(date); } catch (Exception e) { } } return myDate; } /** * 將日期字符串轉化為日期。失敗返回null。 * @param date 日期字符串 * @param dateStyle 日期風格 * @return 日期 */ public static Date formatStringToDate(String date, DateStyle dateStyle) { Date myDate = null; if (dateStyle != null) { date = dateStyle.getValue().equals("yyyy-MM-dd'T'HH:mm:ss.SSS Z")?date.replace("Z", " UTC"):date;myDate = formatStringToDate(date, dateStyle.getValue()); } return myDate; } /** * 將日期轉化為日期字符串。失敗返回null。 * @param date 日期 * @param pattern 日期格式 * @return 日期字符串 */ public static String formatDateToString(Date date, String pattern) { String dateString = null; if (date != null) { try { dateString = getDateFormat(pattern).format(date); } catch (Exception e) { } } return dateString; } /** * 將日期轉化為日期字符串。失敗返回null。 * @param date 日期 * @param dateStyle 日期風格 * @return 日期字符串 */ public static String formatDateToString(Date date, DateStyle dateStyle) { String dateString = null; if (dateStyle != null) { dateString = formatDateToString(date, dateStyle.getValue()); } return dateString; } /** * 將日期轉換為指定格式。 * @param date 日期 * @param dateStyle 日期風格 * @return 日期字符串 */ public static Date formatDateToDate(Date date, DateStyle dateStyle) {Date myDate = null; if (dateStyle != null) { String dateStr = formatDateToString(date, dateStyle);myDate = formatStringToDate(dateStr, dateStyle.getValue()); } return myDate; }/** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param newPattern 新日期格式 * @return 新日期字符串 */ public static String formatStringToString(String date, String newPattern) { DateStyle oldDateStyle = getDateStyle(date); return formatStringToString(date, oldDateStyle, newPattern); } /** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param newDateStyle 新日期風格 * @return 新日期字符串 */ public static String formatStringToString(String date, DateStyle newDateStyle) { DateStyle oldDateStyle = getDateStyle(date); return formatStringToString(date, oldDateStyle, newDateStyle); } /** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param olddPattern 舊日期格式 * @param newPattern 新日期格式 * @return 新日期字符串 */ public static String formatStringToString(String date, String olddPattern, String newPattern) { return formatDateToString(formatStringToDate(date, olddPattern), newPattern); } /** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param olddDteStyle 舊日期風格 * @param newParttern 新日期格式 * @return 新日期字符串 */ public static String formatStringToString(String date, DateStyle olddDteStyle, String newParttern) { String dateString = null; if (olddDteStyle != null) { dateString = formatStringToString(date, olddDteStyle.getValue(), newParttern); } return dateString; } /** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param olddPattern 舊日期格式 * @param newDateStyle 新日期風格 * @return 新日期字符串 */ public static String formatStringToString(String date, String olddPattern, DateStyle newDateStyle) { String dateString = null; if (newDateStyle != null) { dateString = formatStringToString(date, olddPattern, newDateStyle.getValue()); } return dateString; } /** * 將日期字符串轉化為另一日期字符串。失敗返回null。 * @param date 舊日期字符串 * @param olddDteStyle 舊日期風格 * @param newDateStyle 新日期風格 * @return 新日期字符串 */ public static String formatStringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) { String dateString = null; if (olddDteStyle != null && newDateStyle != null) { dateString = formatStringToString(date, olddDteStyle.getValue(), newDateStyle.getValue()); } return dateString; } /** * 增加日期的年份。失敗返回null。 * @param date 日期 * @param yearAmount 增加數量。可為負數 * @return 增加年份后的日期字符串 */ public static String addYear(String date, int yearAmount) { return addInteger(date, Calendar.YEAR, yearAmount); } /** * 增加日期的年份。失敗返回null。 * @param date 日期 * @param yearAmount 增加數量。可為負數 * @return 增加年份后的日期 */ public static Date addYear(Date date, int yearAmount) { return addInteger(date, Calendar.YEAR, yearAmount); } /** * 增加日期的月份。失敗返回null。 * @param date 日期 * @param monthAmount 增加數量。可為負數 * @return 增加月份后的日期字符串 */ public static String addMonth(String date, int monthAmount) { return addInteger(date, Calendar.MONTH, monthAmount); } /** * 增加日期的月份。失敗返回null。 * @param date 日期 * @param monthAmount 增加數量。可為負數 * @return 增加月份后的日期 */ public static Date addMonth(Date date, int monthAmount) { return addInteger(date, Calendar.MONTH, monthAmount); } /** * 增加日期的天數。失敗返回null。 * @param date 日期字符串 * @param dayAmount 增加數量。可為負數 * @return 增加天數后的日期字符串 */ public static String addDay(String date, int dayAmount) { return addInteger(date, Calendar.DATE, dayAmount); } /** * 增加日期的天數。失敗返回null。 * @param date 日期 * @param dayAmount 增加數量。可為負數 * @return 增加天數后的日期 */ public static Date addDay(Date date, int dayAmount) { return addInteger(date, Calendar.DATE, dayAmount); } /** * 增加日期的小時。失敗返回null。 * @param date 日期字符串 * @param hourAmount 增加數量。可為負數 * @return 增加小時后的日期字符串 */ public static String addHour(String date, int hourAmount) { return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount); } /** * 增加日期的小時。失敗返回null。 * @param date 日期 * @param hourAmount 增加數量。可為負數 * @return 增加小時后的日期 */ public static Date addHour(Date date, int hourAmount) { return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount); } /** * 增加日期的分鐘。失敗返回null。 * @param date 日期字符串 * @param minuteAmount 增加數量。可為負數 * @return 增加分鐘后的日期字符串 */ public static String addMinute(String date, int minuteAmount) { return addInteger(date, Calendar.MINUTE, minuteAmount); } /** * 增加日期的分鐘。失敗返回null。 * @param date 日期 * @param dayAmount 增加數量。可為負數 * @return 增加分鐘后的日期 */ public static Date addMinute(Date date, int minuteAmount) { return addInteger(date, Calendar.MINUTE, minuteAmount); } /** * 增加日期的秒鐘。失敗返回null。 * @param date 日期字符串 * @param dayAmount 增加數量。可為負數 * @return 增加秒鐘后的日期字符串 */ public static String addSecond(String date, int secondAmount) { return addInteger(date, Calendar.SECOND, secondAmount); } /** * 增加日期的秒鐘。失敗返回null。 * @param date 日期 * @param dayAmount 增加數量。可為負數 * @return 增加秒鐘后的日期 */ public static Date addSecond(Date date, int secondAmount) { return addInteger(date, Calendar.SECOND, secondAmount); } /** * 獲取日期的年份。失敗返回0。 * @param date 日期字符串 * @return 年份 */ public static int getYear(String date) { return getYear(formatStringToDate(date)); } /** * 獲取日期的年份。失敗返回0。 * @param date 日期 * @return 年份 */ public static int getYear(Date date) { return getInteger(date, Calendar.YEAR); } /** * 獲取日期的月份。失敗返回0。 * @param date 日期字符串 * @return 月份 */ public static int getMonth(String date) { return getMonth(formatStringToDate(date)); } /** * 獲取日期的月份。失敗返回0。 * @param date 日期 * @return 月份 */ public static int getMonth(Date date) { return getInteger(date, Calendar.MONTH) + 1; } /*** 功能說明:獲取月份* 修改說明:* @author 霸道流氓氣質* @date * @param date* @return*/public static Month getMonthEnum(String date) {Month month = null; DateStyle dateStyle = getDateStyle(date); if (dateStyle != null) { Date myDate = formatStringToDate(date, dateStyle); month = getMonthEnum(myDate); } return month; } /*** 功能說明:獲取月份* 修改說明:* @author 霸道流氓氣質* @date */public static Month getMonthEnum(Date date){Month month = null; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int monthNumber = calendar.get(Calendar.MONTH); switch (monthNumber) { case 0: month = month.JANUARY; break; case 1: month = month.FEBRUARY; break; case 2: month = month.MARCH; break; case 3: month = month.APRIL; break; case 4: month = month.MAY; break; case 5: month = month.JUNE; break; case 6: month = month.JULY; break; case 7: month = month.AUGUST; break; case 8: month = month.SEPTEMBER; break; case 9: month = month.OCTOBER; break; case 10: month = month.NOVEMBER; break; case 11: month = month.DECEMBER; break; } return month; }/** * 獲取日期的天數。失敗返回0。 * @param date 日期字符串 * @return 天 */ public static int getDay(String date) { return getDay(formatStringToDate(date)); } /** * 獲取日期的天數。失敗返回0。 * @param date 日期 * @return 天 */ public static int getDay(Date date) { return getInteger(date, Calendar.DATE); } /** * 獲取日期的小時。失敗返回0。 * @param date 日期字符串 * @return 小時 */ public static int getHour(String date) { return getHour(formatStringToDate(date)); } /** * 獲取日期的小時。失敗返回0。 * @param date 日期 * @return 小時 */ public static int getHour(Date date) { return getInteger(date, Calendar.HOUR_OF_DAY); } /** * 獲取日期的分鐘。失敗返回0。 * @param date 日期字符串 * @return 分鐘 */ public static int getMinute(String date) { return getMinute(formatStringToDate(date)); } /** * 獲取日期的分鐘。失敗返回0。 * @param date 日期 * @return 分鐘 */ public static int getMinute(Date date) { return getInteger(date, Calendar.MINUTE); } /** * 獲取日期的秒鐘。失敗返回0。 * @param date 日期字符串 * @return 秒鐘 */ public static int getSecond(String date) { return getSecond(formatStringToDate(date)); } /** * 獲取日期的秒鐘。失敗返回0。 * @param date 日期 * @return 秒鐘 */ public static int getSecond(Date date) { return getInteger(date, Calendar.SECOND); } /** * 獲取日期 。默認yyyy-MM-dd格式。失敗返回null。 * @param date 日期字符串 * @return 日期 */ public static String getDate(String date) { return formatStringToString(date, DateStyle.YYYY_MM_DD); } /** * 獲取日期。默認yyyy-MM-dd格式。失敗返回null。 * @param date 日期 * @return 日期 */ public static String getDate(Date date) { return formatDateToString(date, DateStyle.YYYY_MM_DD); } /** * 獲取日期的時間。默認HH:mm:ss格式。失敗返回null。 * @param date 日期字符串 * @return 時間 */ public static String getTime(String date) { return formatStringToString(date, DateStyle.HH_MM_SS); } /** * 獲取日期的時間。默認HH:mm:ss格式。失敗返回null。 * @param date 日期 * @return 時間 */ public static String getTime(Date date) { return formatDateToString(date, DateStyle.HH_MM_SS); } /** * 獲取日期的星期。失敗返回null。 * @param date 日期字符串 * @return 星期 */ public static Week getWeek(String date) { Week week = null; DateStyle dateStyle = getDateStyle(date); if (dateStyle != null) { Date myDate = formatStringToDate(date, dateStyle); week = getWeek(myDate); } return week; } /** * 獲取日期的星期。失敗返回null。 * @param date 日期 * @return 星期 */ public static Week getWeek(Date date) { Week week = null; Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1; switch (weekNumber) { case 0: week = Week.SUNDAY; break; case 1: week = Week.MONDAY; break; case 2: week = Week.TUESDAY; break; case 3: week = Week.WEDNESDAY; break; case 4: week = Week.THURSDAY; break; case 5:week = Week.FRIDAY;break; case 6: week = Week.SATURDAY; break; } return week; } /** * 獲取兩個日期相差的天數 * @param date 日期 * @param otherDate 另一個日期 */ public static int getDifferDaysNoAbs(Date date, Date otherDate) { int num = 0; Date dateTmp = formatStringToDate(getDate(date), DateStyle.YYYY_MM_DD); Date otherDateTmp = formatStringToDate(getDate(otherDate), DateStyle.YYYY_MM_DD); if (dateTmp != null && otherDateTmp != null) { long time = dateTmp.getTime() - otherDateTmp.getTime(); num = (int) (time / (24 * 60 * 60 * 1000)); } return num; } /** * 獲取兩個日期相差的天數 * @param date 日期 * @param otherDate 另一個日期 */ public static int getDifferDays(Date date, Date otherDate) { int num = 0; Date dateTmp = formatStringToDate(getDate(date), DateStyle.YYYY_MM_DD); Date otherDateTmp = formatStringToDate(getDate(otherDate), DateStyle.YYYY_MM_DD); if (dateTmp != null && otherDateTmp != null) { long time = Math.abs(dateTmp.getTime() - otherDateTmp.getTime()); num = (int) (time / (24 * 60 * 60 * 1000)); } return num; } /** * 獲取兩個日期相差的* @param date 日期字符串 * @param otherDate 另一個日期字符串 * @return 相差天數。如果失敗則返回-1 */ public static int getDifferDaysNoAbs(String date, String otherDate) { return getDifferDaysNoAbs(formatStringToDate(date), formatStringToDate(otherDate)); }/** * 獲取兩個日期相差的* @param date 日期字符串 * @param otherDate 另一個日期字符串 * @return 相差天數。如果失敗則返回-1 */ public static int getDifferDays(String date, String otherDate) { return getDifferDays(formatStringToDate(date), formatStringToDate(otherDate)); }/** * 獲取兩個日期相差的毫秒數 * @param date 日期 * @param otherDate 另一個日期 * @return 相差天數。如果失敗則返回-1 */ public static long getDifferMilliSecond(Date date, Date otherDate) { long time = 0;Date dateTmp = formatStringToDate(getDate(date),DateStyle.YYYY_MM_DD);Date otherDateTmp = formatStringToDate(getDate(otherDate),DateStyle.YYYY_MM_DD);if (dateTmp != null && otherDateTmp != null) {time = dateTmp.getTime() - otherDateTmp.getTime();}return time; } /** * 獲取兩個日期相差的毫秒數 * @param date 日期 * @param otherDate 另一個日期 * @return 相差天數。如果失敗則返回-1 */ public static long getDifferMilliSecond(String date, String otherDate) { return getDifferMilliSecond(formatStringToDate(date), formatStringToDate(otherDate)); }/** * @Title:getDaysListBetweenDates * @Description: 獲得兩個日期之間的連續日期. * @param begin 開始日期 * @param end 結束日期* @param outFormat 輸出時間格式* @return * @return List<String> */ public static List<String> getDaysListBetweenTwoDates(String begin, String end,String outFormat) { List<String> dateList = new ArrayList<String>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(formatDateToString(beginDate,outFormat)); beginDate = addDay(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /** * @Title:getDaysListBetweenDates * @Description: 獲得兩個日期之間的連續日期. * @param begin 開始日期 * @param end 結束日期* @param outFormat 輸出時間格式* @return * @return List<String> */ public static List<String> getDaysListBetweenTwoDates(String begin, String end,DateStyle outFormat) { List<String> dateList = new ArrayList<String>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(formatDateToString(beginDate,outFormat)); beginDate = addDay(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /** * @Title:getDaysListBetweenDates * @Description: 獲得兩個日期之間的連續日期. * @param begin 開始日期 * @param end 結束日期* @param outFormat 輸出時間格式* @return * @return List<String> */ public static List<Date> getDaysListBetweenTwoDatesByDateType(String begin, String end,String outFormat) { List<Date> dateList = new ArrayList<Date>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(beginDate); beginDate = addDay(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /** * @Title:getDaysListBetweenDates * @Description: 獲得兩個日期之間的連續日期. * @param begin 開始日期 * @param end 結束日期* @param outFormat 輸出時間格式* @return * @return List<String> */ public static List<Date> getDaysListBetweenTwoDatesByDateType(String begin, String end,DateStyle outFormat) { List<Date> dateList = new ArrayList<Date>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(beginDate); beginDate = addDay(beginDate, 1);} while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /*** <p>Title: </p>* <p>Description:將時間轉換為時間戳</p>* <p>Company: mesnac</p>* @param s* @return* @throws ParseException* @author 霸道流氓氣質* @date * @retrun*/public static Integer transForMilliSecondByDate(Date date){ if(date==null) return null; return (int)(date.getTime()); } /*** <p>Title: </p>* <p>Description:將時間轉換為時間戳</p>* <p>Company: mesnac</p>* @param date* @return* @author wuzq* @date 2017-5-2上午12:30:25* @retrun*/public static Integer transForMilliSecondByString(String date){ if(date==null) return null; return (int)(formatStringToDate(date).getTime()); }/*** 功能說明:獲取連續月份* 修改說明:數據樣板說明:2017-07-01,2017-08-01,2017-09-01......* @author 霸道流氓氣質* @date * @param begin 開始時間* @param end 結束時間* @param outFormat 日期格式* @return List<String>*/public static List<String> getMonthsListBetweenTwoDatesToString(String begin, String end,String outFormat){List<String> dateList = new ArrayList<String>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(formatDateToString(beginDate,outFormat)); beginDate = addMonth(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; }/*** 功能說明:獲取連續月份* 修改說明:數據樣板說明:2017-07-01,2017-08-01,2017-09-01......* @author 霸道流氓氣質* @date * @param begin 開始時間* @param end 結束時間* @param outFormat 日期格式* @return List<String>*/ public static List<String> getMonthsListBetweenTwoDatesToString(String begin, String end,DateStyle outFormat) { List<String> dateList = new ArrayList<String>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(formatDateToString(beginDate,outFormat)); beginDate = addMonth(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /*** 功能說明:獲取連續月份* 修改說明:數據樣板說明:2017-07-01,2017-08-01,2017-09-01......* @author 霸道流氓氣質* @date * @param begin 開始時間* @param end 結束時間* @param outFormat 日期格式* @return List<Date>*/public static List<Date> getMonthsListBetweenTwoDatesToDate(String begin, String end,String outFormat) { List<Date> dateList = new ArrayList<Date>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(beginDate); beginDate = addMonth(beginDate, 1); } while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; } /*** 功能說明:獲取連續月份* 修改說明:數據樣板說明:2017-07-01,2017-08-01,2017-09-01......* @author 霸道流氓氣質* @date * @param begin 開始時間* @param end 結束時間* @param outFormat 日期格式* @return List<Date>*/public static List<Date> getMonthsListBetweenTwoDatesToDate(String begin, String end,DateStyle outFormat) { List<Date> dateList = new ArrayList<Date>(); try { begin = formatStringToString(begin, outFormat);end = formatStringToString(end, outFormat);Date beginDate = formatStringToDate(begin, outFormat); Date endDate = formatStringToDate(end, outFormat); if (beginDate.compareTo(endDate) > 0) { return dateList; } do { dateList.add(beginDate); beginDate = addMonth(beginDate, 1);} while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) { e.printStackTrace(); } return dateList; }/*** 功能說明:兩個時間相差距離中文描述 * 修改說明:* @author 霸道流氓氣質* @date * @param str1 時間參數 1 格式:1990-01-01 12:00:00 * @param str2 時間參數 2 格式:2009-01-01 12:00:00 * @return */public static String getDistanceTime(String str1, String str2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; long day = 0; long hour = 0; long min = 0; long sec = 0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); sec = (diff/1000-day*24*60*60-hour*60*60-min*60); } catch (ParseException e) { e.printStackTrace(); } if(0!=day) {return day + "天" + hour + "小時" + min + "分" + sec + "秒"; }if(0!=hour) {return hour + "小時" + min + "分" + sec + "秒"; }if(0!=min) {return min + "分" + sec + "秒"; }return sec + "秒"; } /*** 功能說明:兩個時間相差距離中文描述 * 修改說明:* @author 霸道流氓氣質* @date * @param str1 時間參數 1 格式:1990-01-01 12:00:00 * @param str2 時間參數 2 格式:2009-01-01 12:00:00 * @return */public static String getDistanceTimeWithOutSec(String str1, String str2) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date one; Date two; long day = 0; long hour = 0; long min = 0; try { one = df.parse(str1); two = df.parse(str2); long time1 = one.getTime(); long time2 = two.getTime(); long diff ; if(time1<time2) { diff = time2 - time1; } else { diff = time1 - time2; } day = diff / (24 * 60 * 60 * 1000); hour = (diff / (60 * 60 * 1000) - day * 24); min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); } catch (ParseException e) { e.printStackTrace(); } if(0!=day) {return day + "天" + hour + "小時" + min + "分"; }if(0!=hour) {return hour + "小時" + min + "分"; }return min + "分"; } /*** 功能說明:比較兩個時間的大小* 修改說明:* @author 霸道流氓氣質* @date * @param date* @param anotherDate* @param yyyyMmDdHhMm* @return*/public static int compare(String date, String anotherDate,DateStyle yyyyMmDdHhMm) {Date tmpDate = DateConvert.formatStringToDate(date, yyyyMmDdHhMm); Date tmpAnotherDate = DateConvert.formatStringToDate(anotherDate, yyyyMmDdHhMm);return tmpDate.compareTo(tmpAnotherDate);}/*** 功能說明:比較兩個時間的大小* 修改說明:* @author 霸道流氓氣質* @date * @param date* @param anotherDate* @param yyyyMmDdHhMm* @return*/public static int compare(Date date, Date anotherDate,DateStyle yyyyMmDdHhMm) {Date tmpDate = DateConvert.formatDateToDate(date, yyyyMmDdHhMm); Date tmpAnotherDate = DateConvert.formatDateToDate(anotherDate, yyyyMmDdHhMm);return tmpDate.compareTo(tmpAnotherDate);}}?
總結
以上是生活随笔為你收集整理的Java中封装的全局日期处理工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatisPlus条件构造器带条件查
- 下一篇: Java中File类的createNew