|
@@ -18,9 +18,9 @@ import java.util.*;
|
|
|
@Slf4j
|
|
|
public class DateHelper {
|
|
|
|
|
|
- private static final ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>();
|
|
|
+ private static final ThreadLocal<SimpleDateFormat> THREAD_LOCAL = new ThreadLocal<>();
|
|
|
|
|
|
- private static final Object object = new Object();
|
|
|
+ private static final Object INSTANCE = new Object();
|
|
|
|
|
|
/**
|
|
|
* 获取SimpleDateFormat
|
|
@@ -30,13 +30,13 @@ public class DateHelper {
|
|
|
* @throws RuntimeException 异常:非法日期格式
|
|
|
*/
|
|
|
private static SimpleDateFormat getDateFormat(String pattern) throws RuntimeException {
|
|
|
- SimpleDateFormat dateFormat = threadLocal.get();
|
|
|
+ SimpleDateFormat dateFormat = THREAD_LOCAL.get();
|
|
|
if (dateFormat == null) {
|
|
|
- synchronized (object) {
|
|
|
+ synchronized (INSTANCE) {
|
|
|
if (dateFormat == null) {
|
|
|
dateFormat = new SimpleDateFormat(pattern);
|
|
|
dateFormat.setLenient(false);
|
|
|
- threadLocal.set(dateFormat);
|
|
|
+ THREAD_LOCAL.set(dateFormat);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -73,9 +73,9 @@ public class DateHelper {
|
|
|
String dateString = null;
|
|
|
DateStyle dateStyle = getDateStyle(date);
|
|
|
if (dateStyle != null) {
|
|
|
- Date myDate = StringToDate(date, dateStyle);
|
|
|
+ Date myDate = stringToDate(date, dateStyle);
|
|
|
myDate = addInteger(myDate, dateType, amount);
|
|
|
- dateString = DateToString(myDate, dateStyle);
|
|
|
+ dateString = dateToString(myDate, dateStyle);
|
|
|
}
|
|
|
return dateString;
|
|
|
}
|
|
@@ -124,7 +124,7 @@ public class DateHelper {
|
|
|
private static Date getAccurateDate(List<Long> timestamps) {
|
|
|
Date date = null;
|
|
|
long timestamp = 0;
|
|
|
- Map<Long, long[]> map = new HashMap<Long, long[]>();
|
|
|
+ Map<Long, long[]> map = new HashMap<>(16);
|
|
|
List<Long> absoluteValues = new ArrayList<Long>();
|
|
|
|
|
|
if (timestamps != null && timestamps.size() > 0) {
|
|
@@ -194,8 +194,8 @@ public class DateHelper {
|
|
|
*/
|
|
|
public static DateStyle getDateStyle(String date) {
|
|
|
DateStyle dateStyle = null;
|
|
|
- Map<Long, DateStyle> map = new HashMap<Long, DateStyle>();
|
|
|
- List<Long> timestamps = new ArrayList<Long>();
|
|
|
+ Map<Long, DateStyle> map = new HashMap<>(16);
|
|
|
+ List<Long> timestamps = new ArrayList<>();
|
|
|
for (DateStyle style : DateStyle.values()) {
|
|
|
if (style.isShowOnly()) {
|
|
|
continue;
|
|
@@ -229,9 +229,9 @@ public class DateHelper {
|
|
|
* @param date 日期字符串
|
|
|
* @return 日期
|
|
|
*/
|
|
|
- public static Date StringToDate(String date) {
|
|
|
+ public static Date stringToDate(String date) {
|
|
|
DateStyle dateStyle = getDateStyle(date);
|
|
|
- return StringToDate(date, dateStyle);
|
|
|
+ return stringToDate(date, dateStyle);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -241,7 +241,7 @@ public class DateHelper {
|
|
|
* @param pattern 日期格式
|
|
|
* @return 日期
|
|
|
*/
|
|
|
- public static Date StringToDate(String date, String pattern) {
|
|
|
+ public static Date stringToDate(String date, String pattern) {
|
|
|
Date myDate = null;
|
|
|
if (date != null) {
|
|
|
try {
|
|
@@ -259,10 +259,10 @@ public class DateHelper {
|
|
|
* @param dateStyle 日期风格
|
|
|
* @return 日期
|
|
|
*/
|
|
|
- public static Date StringToDate(String date, DateStyle dateStyle) {
|
|
|
+ public static Date stringToDate(String date, DateStyle dateStyle) {
|
|
|
Date myDate = null;
|
|
|
if (dateStyle != null) {
|
|
|
- myDate = StringToDate(date, dateStyle.getValue());
|
|
|
+ myDate = stringToDate(date, dateStyle.getValue());
|
|
|
}
|
|
|
return myDate;
|
|
|
}
|
|
@@ -274,7 +274,7 @@ public class DateHelper {
|
|
|
* @param pattern 日期格式
|
|
|
* @return 日期字符串
|
|
|
*/
|
|
|
- public static String DateToString(Date date, String pattern) {
|
|
|
+ public static String dateToString(Date date, String pattern) {
|
|
|
String dateString = null;
|
|
|
if (date != null) {
|
|
|
try {
|
|
@@ -292,10 +292,10 @@ public class DateHelper {
|
|
|
* @param dateStyle 日期风格
|
|
|
* @return 日期字符串
|
|
|
*/
|
|
|
- public static String DateToString(Date date, DateStyle dateStyle) {
|
|
|
+ public static String dateToString(Date date, DateStyle dateStyle) {
|
|
|
String dateString = null;
|
|
|
if (dateStyle != null) {
|
|
|
- dateString = DateToString(date, dateStyle.getValue());
|
|
|
+ dateString = dateToString(date, dateStyle.getValue());
|
|
|
}
|
|
|
return dateString;
|
|
|
}
|
|
@@ -307,9 +307,9 @@ public class DateHelper {
|
|
|
* @param newPattern 新日期格式
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, String newPattern) {
|
|
|
+ public static String stringToString(String date, String newPattern) {
|
|
|
DateStyle oldDateStyle = getDateStyle(date);
|
|
|
- return StringToString(date, oldDateStyle, newPattern);
|
|
|
+ return stringToString(date, oldDateStyle, newPattern);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -319,9 +319,9 @@ public class DateHelper {
|
|
|
* @param newDateStyle 新日期风格
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, DateStyle newDateStyle) {
|
|
|
+ public static String stringToString(String date, DateStyle newDateStyle) {
|
|
|
DateStyle oldDateStyle = getDateStyle(date);
|
|
|
- return StringToString(date, oldDateStyle, newDateStyle);
|
|
|
+ return stringToString(date, oldDateStyle, newDateStyle);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -332,8 +332,8 @@ public class DateHelper {
|
|
|
* @param newPattern 新日期格式
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, String olddPattern, String newPattern) {
|
|
|
- return DateToString(StringToDate(date, olddPattern), newPattern);
|
|
|
+ public static String stringToString(String date, String olddPattern, String newPattern) {
|
|
|
+ return dateToString(stringToDate(date, olddPattern), newPattern);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -344,10 +344,10 @@ public class DateHelper {
|
|
|
* @param newParttern 新日期格式
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, DateStyle olddDteStyle, String newParttern) {
|
|
|
+ public static String stringToString(String date, DateStyle olddDteStyle, String newParttern) {
|
|
|
String dateString = null;
|
|
|
if (olddDteStyle != null) {
|
|
|
- dateString = StringToString(date, olddDteStyle.getValue(), newParttern);
|
|
|
+ dateString = stringToString(date, olddDteStyle.getValue(), newParttern);
|
|
|
}
|
|
|
return dateString;
|
|
|
}
|
|
@@ -360,10 +360,10 @@ public class DateHelper {
|
|
|
* @param newDateStyle 新日期风格
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, String olddPattern, DateStyle newDateStyle) {
|
|
|
+ public static String stringToString(String date, String olddPattern, DateStyle newDateStyle) {
|
|
|
String dateString = null;
|
|
|
if (newDateStyle != null) {
|
|
|
- dateString = StringToString(date, olddPattern, newDateStyle.getValue());
|
|
|
+ dateString = stringToString(date, olddPattern, newDateStyle.getValue());
|
|
|
}
|
|
|
return dateString;
|
|
|
}
|
|
@@ -376,10 +376,10 @@ public class DateHelper {
|
|
|
* @param newDateStyle 新日期风格
|
|
|
* @return 新日期字符串
|
|
|
*/
|
|
|
- public static String StringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) {
|
|
|
+ public static String stringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) {
|
|
|
String dateString = null;
|
|
|
if (olddDteStyle != null && newDateStyle != null) {
|
|
|
- dateString = StringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
|
|
|
+ dateString = stringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
|
|
|
}
|
|
|
return dateString;
|
|
|
}
|
|
@@ -523,7 +523,7 @@ public class DateHelper {
|
|
|
* @return 年份
|
|
|
*/
|
|
|
public static int getYear(String date) {
|
|
|
- return getYear(StringToDate(date));
|
|
|
+ return getYear(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -543,7 +543,7 @@ public class DateHelper {
|
|
|
* @return 月份
|
|
|
*/
|
|
|
public static int getMonth(String date) {
|
|
|
- return getMonth(StringToDate(date));
|
|
|
+ return getMonth(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -563,7 +563,7 @@ public class DateHelper {
|
|
|
* @return 天
|
|
|
*/
|
|
|
public static int getDay(String date) {
|
|
|
- return getDay(StringToDate(date));
|
|
|
+ return getDay(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -583,7 +583,7 @@ public class DateHelper {
|
|
|
* @return 小时
|
|
|
*/
|
|
|
public static int getHour(String date) {
|
|
|
- return getHour(StringToDate(date));
|
|
|
+ return getHour(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -603,7 +603,7 @@ public class DateHelper {
|
|
|
* @return 分钟
|
|
|
*/
|
|
|
public static int getMinute(String date) {
|
|
|
- return getMinute(StringToDate(date));
|
|
|
+ return getMinute(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -623,7 +623,7 @@ public class DateHelper {
|
|
|
* @return 秒钟
|
|
|
*/
|
|
|
public static int getSecond(String date) {
|
|
|
- return getSecond(StringToDate(date));
|
|
|
+ return getSecond(stringToDate(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -643,7 +643,7 @@ public class DateHelper {
|
|
|
* @return 日期
|
|
|
*/
|
|
|
public static String getDate(String date) {
|
|
|
- return StringToString(date, DateStyle.YYYY_MM_DD);
|
|
|
+ return stringToString(date, DateStyle.YYYY_MM_DD);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -653,7 +653,7 @@ public class DateHelper {
|
|
|
* @return 日期
|
|
|
*/
|
|
|
public static String getDate(Date date) {
|
|
|
- return DateToString(date, DateStyle.YYYY_MM_DD);
|
|
|
+ return dateToString(date, DateStyle.YYYY_MM_DD);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -663,7 +663,7 @@ public class DateHelper {
|
|
|
* @return 时间
|
|
|
*/
|
|
|
public static String getTime(String date) {
|
|
|
- return StringToString(date, DateStyle.HH_MM_SS);
|
|
|
+ return stringToString(date, DateStyle.HH_MM_SS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -673,7 +673,7 @@ public class DateHelper {
|
|
|
* @return 时间
|
|
|
*/
|
|
|
public static String getTime(Date date) {
|
|
|
- return DateToString(date, DateStyle.HH_MM_SS);
|
|
|
+ return dateToString(date, DateStyle.HH_MM_SS);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -684,7 +684,7 @@ public class DateHelper {
|
|
|
* @return 相差天数。如果失败则返回-1
|
|
|
*/
|
|
|
public static int getIntervalDays(String date, String otherDate) {
|
|
|
- return getIntervalDays(StringToDate(date), StringToDate(otherDate));
|
|
|
+ return getIntervalDays(stringToDate(date), stringToDate(otherDate));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -694,8 +694,8 @@ public class DateHelper {
|
|
|
*/
|
|
|
public static int getIntervalDays(Date date, Date otherDate) {
|
|
|
int num = -1;
|
|
|
- Date dateTmp = DateHelper.StringToDate(DateHelper.getDate(date), DateStyle.YYYY_MM_DD);
|
|
|
- Date otherDateTmp = DateHelper.StringToDate(DateHelper.getDate(otherDate), DateStyle.YYYY_MM_DD);
|
|
|
+ Date dateTmp = DateHelper.stringToDate(DateHelper.getDate(date), DateStyle.YYYY_MM_DD);
|
|
|
+ Date otherDateTmp = DateHelper.stringToDate(DateHelper.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));
|
|
@@ -711,7 +711,7 @@ public class DateHelper {
|
|
|
*/
|
|
|
public static Date getDayStart(Date date) {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
- return StringToDate(sdf.format(date));
|
|
|
+ return stringToDate(sdf.format(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -722,7 +722,7 @@ public class DateHelper {
|
|
|
*/
|
|
|
public static Date getDayEnd(Date date) {
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
- return StringToDate(sdf.format(date));
|
|
|
+ return stringToDate(sdf.format(date));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -731,11 +731,12 @@ public class DateHelper {
|
|
|
* @return
|
|
|
*/
|
|
|
public static Date getCurrentWeekStart(Date date) {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>(16);
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
- cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期
|
|
|
- return StringToDate(df.format(cal.getTime()));
|
|
|
+ // 获取本周一的日期
|
|
|
+ cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
|
+ return stringToDate(df.format(cal.getTime()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -744,13 +745,13 @@ public class DateHelper {
|
|
|
* @return
|
|
|
*/
|
|
|
public static Date getCurrentWeekEnd() {
|
|
|
- Map<String, String> map = new HashMap<String, String>();
|
|
|
+ Map<String, String> map = new HashMap<>(16);
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
|
|
// 增加一个星期,才是我们中国人理解的本周日的日期
|
|
|
cal.add(Calendar.WEEK_OF_YEAR, 1);
|
|
|
- return StringToDate(df.format(cal.getTime()));
|
|
|
+ return stringToDate(df.format(cal.getTime()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -759,11 +760,13 @@ public class DateHelper {
|
|
|
* @return
|
|
|
*/
|
|
|
public static Date getCurrentMonthStart() {
|
|
|
- Calendar cal = Calendar.getInstance();// 获取当前日期
|
|
|
+ // 获取当前日期
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
cal.add(Calendar.MONTH, 0);
|
|
|
- cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
|
|
|
+ // 设置为1号,当前日期既为本月第一天
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
- return StringToDate(df.format(cal.getTime()));
|
|
|
+ return stringToDate(df.format(cal.getTime()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -776,7 +779,7 @@ public class DateHelper {
|
|
|
// 设置为1号,当前日期既为本月第一天
|
|
|
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
|
|
- return StringToDate(df.format(cal.getTime()));
|
|
|
+ return stringToDate(df.format(cal.getTime()));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -787,17 +790,19 @@ public class DateHelper {
|
|
|
* @return
|
|
|
*/
|
|
|
public static Date getMonthStart(String strYear, String strMonth) {
|
|
|
- Calendar cal = Calendar.getInstance();// 获取当前日期
|
|
|
+ // 获取当前日期
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
cal.set(Calendar.YEAR, Integer.parseInt(strYear));
|
|
|
cal.set(Calendar.MONTH, Integer.parseInt(strMonth));
|
|
|
- cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
|
|
|
+ // 设置为1号,当前日期既为本月第一天
|
|
|
+ cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
|
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
Date firstDate = cal.getTime();
|
|
|
|
|
|
- return StringToDate(df.format(firstDate));
|
|
|
+ return stringToDate(df.format(firstDate));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -817,7 +822,7 @@ public class DateHelper {
|
|
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
cal.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
Date lastDate = cal.getTime();
|
|
|
- return StringToDate(df.format(lastDate));
|
|
|
+ return stringToDate(df.format(lastDate));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -872,7 +877,8 @@ public class DateHelper {
|
|
|
list = new ArrayList<String>();
|
|
|
|
|
|
String firstDay = StrUtil.EMPTY;
|
|
|
- Date date = new SimpleDateFormat("yyyy-MM").parse(month);// 定义起始日期
|
|
|
+ // 定义起始日期
|
|
|
+ Date date = new SimpleDateFormat("yyyy-MM").parse(month);
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
|
@@ -880,13 +886,13 @@ public class DateHelper {
|
|
|
date = now;
|
|
|
}
|
|
|
|
|
|
- List<String> mlist = getYearMonthBetweenDate(DateToString(date, "yyyy-MM"), DateToString(now, "yyyy-MM"));
|
|
|
+ List<String> mlist = getYearMonthBetweenDate(dateToString(date, "yyyy-MM"), dateToString(now, "yyyy-MM"));
|
|
|
|
|
|
if (mlist.size() <= offset) {
|
|
|
int preOffSet = offset * 2 + 1 - mlist.size();
|
|
|
for (int i = preOffSet; i >= 1; i--) {
|
|
|
Date d = addMonth(date, i * -1);
|
|
|
- list.add(DateToString(d, "yyyy-MM"));
|
|
|
+ list.add(dateToString(d, "yyyy-MM"));
|
|
|
}
|
|
|
list.addAll(mlist);
|
|
|
return list;
|
|
@@ -895,7 +901,7 @@ public class DateHelper {
|
|
|
// 往前数n月
|
|
|
for (int i = offset; i >= 1; i--) {
|
|
|
Date d = addMonth(date, i * -1);
|
|
|
- list.add(DateToString(d, "yyyy-MM"));
|
|
|
+ list.add(dateToString(d, "yyyy-MM"));
|
|
|
}
|
|
|
|
|
|
list.add(month);
|
|
@@ -903,7 +909,7 @@ public class DateHelper {
|
|
|
// 往后数n月
|
|
|
for (int i = 1; i <= offset; i++) {
|
|
|
Date d = addMonth(date, i);
|
|
|
- list.add(DateToString(d, "yyyy-MM"));
|
|
|
+ list.add(dateToString(d, "yyyy-MM"));
|
|
|
}
|
|
|
|
|
|
} catch (ParseException e) {
|
|
@@ -913,61 +919,53 @@ public class DateHelper {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取两个日期间的年月
|
|
|
+ * @param startDate
|
|
|
+ * @param endDate
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public static List<String> getYearMonthBetweenDate(String startDate, String endDate) {
|
|
|
- List<String> list = null;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
try {
|
|
|
- list = new ArrayList<String>();
|
|
|
-
|
|
|
- String firstDay = StrUtil.EMPTY;
|
|
|
- String lastDay = StrUtil.EMPTY;
|
|
|
- Date d1 = new SimpleDateFormat("yyyy-MM").parse(startDate);// 定义起始日期
|
|
|
- Date d2 = new SimpleDateFormat("yyyy-MM").parse(endDate);// 定义结束日期
|
|
|
+ Date d1 = new SimpleDateFormat("yyyy-MM").parse(startDate);
|
|
|
+ Date d2 = new SimpleDateFormat("yyyy-MM").parse(endDate);
|
|
|
|
|
|
- Calendar dd = Calendar.getInstance();// 定义日期实例
|
|
|
- dd.setTime(d1);// 设置日期起始时间
|
|
|
+ Calendar calendar1 = Calendar.getInstance();
|
|
|
+ calendar1.setTime(d1);
|
|
|
+ Calendar calendar2 = Calendar.getInstance();
|
|
|
+ calendar2.setTime(d2);
|
|
|
Calendar cale = Calendar.getInstance();
|
|
|
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTime(d2);
|
|
|
-
|
|
|
- int startDay = d1.getDate();
|
|
|
- int endDay = d2.getDate();
|
|
|
+ int startDay = calendar1.get(Calendar.DAY_OF_MONTH);
|
|
|
+ int endDay = calendar2.get(Calendar.DAY_OF_MONTH);
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
|
|
-
|
|
|
- String keyValueForDate = null;
|
|
|
-
|
|
|
- while (dd.getTime().compareTo(d2) <= 0) {// 判断是否到结束日期
|
|
|
- keyValueForDate = new String();
|
|
|
- cale.setTime(dd.getTime());
|
|
|
-
|
|
|
- if (dd.getTime().equals(d1)) {
|
|
|
- cale.set(Calendar.DAY_OF_MONTH, dd.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
- keyValueForDate = sdf.format(d1);
|
|
|
-
|
|
|
- } else if (dd.get(Calendar.MONTH) == d2.getMonth() && dd.get(Calendar.YEAR) == c.get(Calendar.YEAR)) {
|
|
|
- cale.set(Calendar.DAY_OF_MONTH, 1);// 取第一天
|
|
|
- firstDay = sdf.format(cale.getTime());
|
|
|
- keyValueForDate = firstDay;
|
|
|
-
|
|
|
+ // 判断是否到结束日期
|
|
|
+ while (calendar1.compareTo(calendar2) <= 0) {
|
|
|
+ cale.setTime(calendar1.getTime());
|
|
|
+ // 如果是同一天
|
|
|
+ if (calendar1.equals(calendar2)) {
|
|
|
+ cale.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
+ list.add(sdf.format(d1));
|
|
|
+ } else if (calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
|
|
|
+ && calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) {
|
|
|
+ // 如果是同月 - 取第一天
|
|
|
+ cale.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ list.add(sdf.format(cale.getTime()));
|
|
|
} else {
|
|
|
- cale.set(Calendar.DAY_OF_MONTH, 1);// 取第一天
|
|
|
- firstDay = sdf.format(cale.getTime());
|
|
|
- cale.set(Calendar.DAY_OF_MONTH, dd.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
- keyValueForDate = firstDay;
|
|
|
-
|
|
|
+ // 非同月 - 取第一天
|
|
|
+ cale.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ list.add(sdf.format(cale.getTime()));
|
|
|
+ cale.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH));
|
|
|
}
|
|
|
- list.add(keyValueForDate);
|
|
|
- dd.add(Calendar.MONTH, 1);// 进行当前日期月份加1
|
|
|
+ // 进行当前日期月份加1
|
|
|
+ calendar1.add(Calendar.MONTH, 1);
|
|
|
}
|
|
|
-
|
|
|
if (endDay < startDay) {
|
|
|
- keyValueForDate = new String();
|
|
|
-
|
|
|
cale.setTime(d2);
|
|
|
- cale.set(Calendar.DAY_OF_MONTH, 1);// 取第一天
|
|
|
- firstDay = sdf.format(cale.getTime());
|
|
|
- keyValueForDate = firstDay;
|
|
|
- list.add(keyValueForDate);
|
|
|
+ // 取第一天
|
|
|
+ cale.set(Calendar.DAY_OF_MONTH, 1);
|
|
|
+ list.add(sdf.format(cale.getTime()));
|
|
|
}
|
|
|
} catch (ParseException e) {
|
|
|
return null;
|
|
@@ -980,7 +978,8 @@ public class DateHelper {
|
|
|
startDate.set(Calendar.YEAR, year);
|
|
|
startDate.set(Calendar.MONTH, month-1);
|
|
|
startDate.set(Calendar.DAY_OF_MONTH,1);
|
|
|
- startDate.set(Calendar.HOUR_OF_DAY , 0); //HOUR是12小时制
|
|
|
+ //HOUR是12小时制
|
|
|
+ startDate.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
startDate.set(Calendar.MINUTE, 0);
|
|
|
startDate.set(Calendar.SECOND, 0);
|
|
|
startDate.set(Calendar.MILLISECOND, 0);
|
|
@@ -1015,6 +1014,13 @@ public class DateHelper {
|
|
|
return new BigDecimal(diff / nh).setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 清理缓存
|
|
|
+ */
|
|
|
+ public static void clear() {
|
|
|
+ THREAD_LOCAL.remove();
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
//getMonthWithOffset("2017-03", 3);
|
|
|
// System.out.println(getDateTimeDifferenceHours(
|