|
@@ -26,40 +26,42 @@ import com.jfinal.kit.StrKit;
|
|
|
*/
|
|
*/
|
|
|
public class DateKit {
|
|
public class DateKit {
|
|
|
|
|
|
|
|
- public static String dateFormat = "yyyy-MM-dd";
|
|
|
|
|
- public static String timeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
|
|
+ public static String datePattern = "yyyy-MM-dd";
|
|
|
|
|
+ public static String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
|
|
|
- public static void setDateFromat(String dateFormat) {
|
|
|
|
|
- if (StrKit.isBlank(dateFormat)) {
|
|
|
|
|
- throw new IllegalArgumentException("dateFormat can not be blank.");
|
|
|
|
|
|
|
+ public static void setDatePattern(String datePattern) {
|
|
|
|
|
+ if (StrKit.isBlank(datePattern)) {
|
|
|
|
|
+ throw new IllegalArgumentException("datePattern can not be blank");
|
|
|
}
|
|
}
|
|
|
- DateKit.dateFormat = dateFormat;
|
|
|
|
|
|
|
+ DateKit.datePattern = datePattern;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void setTimeFromat(String timeFormat) {
|
|
|
|
|
- if (StrKit.isBlank(timeFormat)) {
|
|
|
|
|
- throw new IllegalArgumentException("timeFormat can not be blank.");
|
|
|
|
|
|
|
+ public static void setTimeStampPattern(String timeStampPattern) {
|
|
|
|
|
+ if (StrKit.isBlank(timeStampPattern)) {
|
|
|
|
|
+ throw new IllegalArgumentException("timeStampPattern can not be blank");
|
|
|
}
|
|
}
|
|
|
- DateKit.timeFormat = timeFormat;
|
|
|
|
|
|
|
+ DateKit.timeStampPattern = timeStampPattern;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static Date toDate(String dateStr) {
|
|
public static Date toDate(String dateStr) {
|
|
|
- if (dateStr == null || "".equals(dateStr.trim())) {
|
|
|
|
|
|
|
+ if (StrKit.isBlank(dateStr)) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ dateStr = dateStr.trim();
|
|
|
int length = dateStr.length();
|
|
int length = dateStr.length();
|
|
|
try {
|
|
try {
|
|
|
- if (length == timeFormat.length()) {
|
|
|
|
|
- SimpleDateFormat sdfDate = new SimpleDateFormat(timeFormat);
|
|
|
|
|
|
|
+ if (length == timeStampPattern.length()) {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(timeStampPattern);
|
|
|
try {
|
|
try {
|
|
|
- return sdfDate.parse(dateStr);
|
|
|
|
|
|
|
+ return sdf.parse(dateStr);
|
|
|
} catch (ParseException e) {
|
|
} catch (ParseException e) {
|
|
|
dateStr = dateStr.replace(".", "-");
|
|
dateStr = dateStr.replace(".", "-");
|
|
|
dateStr = dateStr.replace("/", "-");
|
|
dateStr = dateStr.replace("/", "-");
|
|
|
- return sdfDate.parse(dateStr);
|
|
|
|
|
|
|
+ return sdf.parse(dateStr);
|
|
|
}
|
|
}
|
|
|
- } else if (length == dateFormat.length()) {
|
|
|
|
|
- SimpleDateFormat sdfDate = new SimpleDateFormat(dateFormat);
|
|
|
|
|
|
|
+ } else if (length == datePattern.length()) {
|
|
|
|
|
+ SimpleDateFormat sdfDate = new SimpleDateFormat(datePattern);
|
|
|
try {
|
|
try {
|
|
|
return sdfDate.parse(dateStr);
|
|
return sdfDate.parse(dateStr);
|
|
|
} catch (ParseException e) {
|
|
} catch (ParseException e) {
|
|
@@ -68,19 +70,25 @@ public class DateKit {
|
|
|
return sdfDate.parse(dateStr);
|
|
return sdfDate.parse(dateStr);
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- throw new IllegalArgumentException("The date format is not supported for the time being.");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("The date format is not supported for the time being");
|
|
|
}
|
|
}
|
|
|
} catch (ParseException e) {
|
|
} catch (ParseException e) {
|
|
|
- throw new IllegalArgumentException("The date format is not supported for the time being.");
|
|
|
|
|
|
|
+ throw new IllegalArgumentException("The date format is not supported for the time being");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static String toStr(Date date) {
|
|
public static String toStr(Date date) {
|
|
|
- return toStr(date, DateKit.dateFormat);
|
|
|
|
|
|
|
+ return toStr(date, DateKit.datePattern);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static String toStr(Date date, String format) {
|
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
|
|
|
|
+ public static String toStr(Date date, String pattern) {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
|
|
return sdf.format(date);
|
|
return sdf.format(date);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|