Browse Source

Update DateUtil.java

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

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

@ -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,23 +43,24 @@ public class DateUtil { @@ -44,23 +43,24 @@ 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.
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
* @return
* @throws CoreException
*/
public static String convert(String dateStr, String srcPattern, String destPattern) throws CoreException{
if (dateStr == null){
public static String convert(String dateStr, String srcPattern, String destPattern) throws CoreException {
if (dateStr == null) {
return null;
}
@ -68,27 +68,54 @@ public class DateUtil { @@ -68,27 +68,54 @@ 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
*/
public static String format(Date date, String pattern){
public static String format(Date date, String pattern) {
DateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
/**
* Format a date with specific String pattern.
*
* @param date
* @param pattern
* @Param locale
* @return
*/
public static String format(Date date, String pattern, Locale locale){
public static String format(Date date, String pattern, Locale locale) {
DateFormat formatter = new SimpleDateFormat(pattern, locale);
return formatter.format(date);
@ -96,12 +123,13 @@ public class DateUtil { @@ -96,12 +123,13 @@ public class DateUtil {
/**
* Parse a string to a date with pattern.
*
* @param src
* @param pattern
* @return
* @throws CoreException
*/
public static Date parse(String src, String pattern) throws CoreException{
public static Date parse(String src, String pattern) throws CoreException {
DateFormat formatter = new SimpleDateFormat(pattern);
try {
return formatter.parse(src);
@ -111,29 +139,28 @@ public class DateUtil { @@ -111,29 +139,28 @@ public class DateUtil {
}
}
public static int compare(String src, String dest, String pattern) throws CoreException{
public static int compare(String src, String dest, String pattern) throws CoreException {
Date srcDate = parse(src, pattern);
Date destDate = parse(dest, pattern);
return srcDate.compareTo(destDate);
}
public static long diff(String src, String dest, String pattern) throws CoreException{
public static long diff(String src, String dest, String pattern) throws CoreException {
Date srcDate = parse(src, pattern);
Date destDate = parse(dest, pattern);
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();
src.setTime(srcDate);
Calendar dest = Calendar.getInstance();
dest.setTime(destDate);
if (src.get(Calendar.YEAR) == dest.get(Calendar.YEAR)){
if (src.get(Calendar.YEAR) == dest.get(Calendar.YEAR)) {
return dest.get(Calendar.DAY_OF_YEAR) - src.get(Calendar.DAY_OF_YEAR);
} else {
int srcYear = src.get(Calendar.YEAR);
@ -143,7 +170,7 @@ public class DateUtil { @@ -143,7 +170,7 @@ public class DateUtil {
tmp.set(Calendar.YEAR, srcYear);
int diff = tmp.get(Calendar.DAY_OF_YEAR) - src.get(Calendar.DAY_OF_YEAR);
diff += (destDate.getTime()-tmp.getTimeInMillis())/(1000*60*60*24L);
diff += (destDate.getTime() - tmp.getTimeInMillis()) / (1000 * 60 * 60 * 24L);
return diff;
}
@ -161,74 +188,86 @@ public class DateUtil { @@ -161,74 +188,86 @@ 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){
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);
}
public static Date hourAdd(Date date, int offset){
public static Date hourAdd(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + offset);
return calendar.getTime();
}
public static Date hourAdd(int offset){
public static Date hourAdd(int 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);
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);
}
public static Date monthAdd(Date date, int offset){
public static Date monthAdd(Date date, int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + offset);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DATE));
return calendar.getTime();
}
public static Date monthAdd(int offset){
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);
}
public static String getFirstDay(Date month, String destPattern){
public static String getFirstDay(Date month, String destPattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(month);
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 getFirstDay(String month, String srcPattern, String destPattern) throws CoreException {
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.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);
public static String getLastDay(String month, String srcPattern, String destPattern) throws CoreException {
return getLastDay(parse(month, srcPattern), destPattern);
}
}

Loading…
Cancel
Save