背景
無論使用什麼開發語言,無論開發那種類型的項目,日期格式化都是繞不過去的一個問題,就我個人感覺java中對日期的格式化操作不如C#來的方便,因此爲了彌補這塊,就整理了一個工具類DateConvert,本文就分享一下。
代碼實現
1、DateStyle.java
package com.mesnac.sys.util;
public enum DateStyle {
YYYY_MM("yyyy-MM", false),
YYYY_MM_DD("yyyy-MM-dd", false),
YYYY_MM_DD_HH_MM("yyyy-MM-dd HH:mm", false),
YYYY_MM_DD_HH_MM_SS("yyyy-MM-dd HH:mm:ss", false),
YYYY("yyyy", false),
YYYYMM("yyyyMM", false),
YYYYMMDD("yyyyMMdd", false),
YYYY_MM_EN("yyyy/MM", false),
YYYY_MM_DD_EN("yyyy/MM/dd", false),
YYYY_MM_DD_HH_MM_EN("yyyy/MM/dd HH:mm", false),
YYYY_MM_DD_HH_MM_SS_EN("yyyy/MM/dd HH:mm:ss", false),
YYYY_MM_CN("yyyy年MM月", false),
YYYY_MM_DD_CN("yyyy年MM月dd日", false),
YYYY_MM_DD_HH_MM_CN("yyyy年MM月dd日 HH:mm", false),
YYYY_MM_DD_HH_MM_SS_CN("yyyy年MM月dd日 HH:mm:ss", false),
HH_MM("HH:mm", true),
HH_MM_SS("HH:mm:ss", true),
MM_DD("MM-dd", true),
MM_DD_HH_MM("MM-dd HH:mm", true),
MM_DD_HH_MM_SS("MM-dd HH:mm:ss", true),
MM_DD_EN("MM/dd", true),
MM_DD_HH_MM_EN("MM/dd HH:mm", true),
MM_DD_HH_MM_SS_EN("MM/dd HH:mm:ss", true),
MM_DD_CN("MM月dd日", true),
MM_DD_HH_MM_CN("MM月dd日 HH:mm", true),
MM_DD_HH_MM_SS_CN("MM月dd日 HH:mm:ss", true);
private String value;
private boolean isShowOnly;
DateStyle(String value, boolean isShowOnly) {
this.value = value;
this.isShowOnly = isShowOnly;
}
public String getValue() {
return value;
}
public boolean isShowOnly() {
return isShowOnly;
}
}
2、DateConvert.java
package com.mesnac.sys.util;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
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 zhenglb,wuzq
* @DateTime 2017-04-18 21:45:35
*/
public class DateConvert implements Converter<String, Date> {
@SuppressWarnings("deprecation")
@Override
public Date convert(String stringDate) {
if (stringDate == null || "".equals(stringDate.trim())) {
return new Date();
}
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 {
ParsePosition pos = new ParsePosition(0);
dateTmp = getDateFormat(style.getValue()).parse(date, pos);
if (pos.getIndex() != date.length()) {
dateTmp = null;
}
} catch (Exception e) {
}
}
if (dateTmp != null) {
timestamps.add(dateTmp.getTime());
map.put(dateTmp.getTime(), style);
}
}
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) {
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;
}
/**
* 將日期字符串轉化爲另一日期字符串。失敗返回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 2017-8-9 下午10:11:20
* @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 2017-8-9 下午9:53:41
*/
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 另一個日期
* @return 相差天數。如果失敗則返回-1
*/
public static int getDifferDays(Date date, Date otherDate) {
int num = -1;
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 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 wuzq
* @date 2017-5-2上午12:21:22
* @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 2017-8-8 上午9:17:05
* @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 2017-8-8 上午9:17:05
* @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 2017-8-8 上午9:17:05
* @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 2017-8-8 上午9:17:05
* @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;
}
}