java对时间处理进行格式化的工具
Java实现日期格式化封装类-DateConvert
- 背景
- 代码实现
背景
无论使用什么开发语言,无论开发那种类型的项目,日期格式化都是绕不过去的一个问题,就我个人感觉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;
}
}
原文链接:https://blog.51cto.com/u_16099247/11452211

浙公网安备 33010602011771号