|
|
|
@ -10,9 +10,8 @@ import java.util.Locale;
@@ -10,9 +10,8 @@ import java.util.Locale;
|
|
|
|
|
|
|
|
|
|
import vip.xumy.core.exception.CoreException; |
|
|
|
|
|
|
|
|
|
/** All rights reserved |
|
|
|
|
* author:mengyxu |
|
|
|
|
* date:2019年5月17日 |
|
|
|
|
/** |
|
|
|
|
* All rights reserved author:mengyxu date:2019年5月17日 |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
public class DateUtil { |
|
|
|
@ -44,15 +43,16 @@ public class DateUtil {
@@ -44,15 +43,16 @@ public class DateUtil {
|
|
|
|
|
|
|
|
|
|
public static final String FORMAT20_LINE_YYYYMMDDTHHMMSS = "yyyy-MM-dd'T'HH:mm:ss"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private DateUtil() { |
|
|
|
|
// Add a private constructor to hide the implicit public one.
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* <p> |
|
|
|
|
* Convert a date string from <code>srcPattern</code> to <code>destPattern</code>。 |
|
|
|
|
* Convert a date string from <code>srcPattern</code> to |
|
|
|
|
* <code>destPattern</code>。 |
|
|
|
|
* </p> |
|
|
|
|
* |
|
|
|
|
* @param dateStr |
|
|
|
|
* @param srcPattern |
|
|
|
|
* @param destPattern |
|
|
|
@ -68,8 +68,35 @@ public class DateUtil {
@@ -68,8 +68,35 @@ public class DateUtil {
|
|
|
|
|
return format(date, destPattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Format now with default String pattern. |
|
|
|
|
* |
|
|
|
|
* @param date |
|
|
|
|
* @param pattern |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static String format() { |
|
|
|
|
DateFormat formatter = new SimpleDateFormat(FORMAT19_LINE_YYYYMMDDHHMMSS); |
|
|
|
|
|
|
|
|
|
return formatter.format(new Date()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Format a date with default String pattern. |
|
|
|
|
* |
|
|
|
|
* @param date |
|
|
|
|
* @param pattern |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static String format(Date date) { |
|
|
|
|
DateFormat formatter = new SimpleDateFormat(FORMAT19_LINE_YYYYMMDDHHMMSS); |
|
|
|
|
|
|
|
|
|
return formatter.format(date); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Format a date with specific String pattern. |
|
|
|
|
* |
|
|
|
|
* @param date |
|
|
|
|
* @param pattern |
|
|
|
|
* @return |
|
|
|
@ -80,9 +107,9 @@ public class DateUtil {
@@ -80,9 +107,9 @@ public class DateUtil {
|
|
|
|
|
return formatter.format(date); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Format a date with specific String pattern. |
|
|
|
|
* |
|
|
|
|
* @param date |
|
|
|
|
* @param pattern |
|
|
|
|
* @Param locale |
|
|
|
@ -96,6 +123,7 @@ public class DateUtil {
@@ -96,6 +123,7 @@ public class DateUtil {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Parse a string to a date with pattern. |
|
|
|
|
* |
|
|
|
|
* @param src |
|
|
|
|
* @param pattern |
|
|
|
|
* @return |
|
|
|
@ -125,7 +153,6 @@ public class DateUtil {
@@ -125,7 +153,6 @@ public class DateUtil {
|
|
|
|
|
return (destDate.getTime() - srcDate.getTime()) / (1000 * 60 * 60 * 24); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int diff(Date srcDate, Date destDate) throws CoreException { |
|
|
|
|
Calendar src = Calendar.getInstance(); |
|
|
|
|
src.setTime(srcDate); |
|
|
|
@ -161,16 +188,19 @@ public class DateUtil {
@@ -161,16 +188,19 @@ public class DateUtil {
|
|
|
|
|
public static Date dayAdd(int offset) { |
|
|
|
|
return dayAdd(new Date(), offset); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Date dayAdd(Date date, int offset) { |
|
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
|
calendar.setTime(date); |
|
|
|
|
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + offset); |
|
|
|
|
return calendar.getTime(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String dayAdd(String pattern, int offset) { |
|
|
|
|
Date date = dayAdd(offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String dayAdd(String src, String pattern, int offset) throws CoreException { |
|
|
|
|
Date date = dayAdd(parse(src, pattern), offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
@ -182,13 +212,16 @@ public class DateUtil {
@@ -182,13 +212,16 @@ public class DateUtil {
|
|
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + offset); |
|
|
|
|
return calendar.getTime(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Date hourAdd(int offset) { |
|
|
|
|
return hourAdd(new Date(), offset); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String hourAdd(String pattern, int offset) { |
|
|
|
|
Date date = hourAdd(offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String hourAdd(String src, String pattern, int offset) throws CoreException { |
|
|
|
|
Date date = hourAdd(parse(src, pattern), offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
@ -201,13 +234,16 @@ public class DateUtil {
@@ -201,13 +234,16 @@ public class DateUtil {
|
|
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DATE)); |
|
|
|
|
return calendar.getTime(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Date monthAdd(int offset) { |
|
|
|
|
return monthAdd(new Date(), offset); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String monthAdd(String pattern, int offset) { |
|
|
|
|
Date date = monthAdd(offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String monthAdd(String src, String pattern, int offset) throws CoreException { |
|
|
|
|
Date date = monthAdd(parse(src, pattern), offset); |
|
|
|
|
return format(date, pattern); |
|
|
|
@ -219,15 +255,18 @@ public class DateUtil {
@@ -219,15 +255,18 @@ public class DateUtil {
|
|
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DATE)); |
|
|
|
|
return format(calendar.getTime(), destPattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getFirstDay(String month, String srcPattern, String destPattern) throws CoreException { |
|
|
|
|
return getFirstDay(parse(month, srcPattern), destPattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getLastDay(Date month, String pattern) { |
|
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
|
calendar.setTime(month); |
|
|
|
|
calendar.set(Calendar.DAY_OF_MONTH, calendar.getMaximum(Calendar.DATE)); |
|
|
|
|
return format(calendar.getTime(), pattern); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getLastDay(String month, String srcPattern, String destPattern) throws CoreException { |
|
|
|
|
return getLastDay(parse(month, srcPattern), destPattern); |
|
|
|
|
} |
|
|
|
|