Browse Source

Update DateUtil.java

for_java17
许孟阳 2 years ago
parent
commit
5d4ac7ba5d
  1. 53
      src/main/java/vip/xumy/core/utils/DateUtil.java

53
src/main/java/vip/xumy/core/utils/DateUtil.java

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

Loading…
Cancel
Save