Browse Source

jfinal 3.2 release

James 8 years ago
parent
commit
9366384c76
1 changed files with 30 additions and 22 deletions
  1. 30 22
      src/main/java/com/jfinal/ext/kit/DateKit.java

+ 30 - 22
src/main/java/com/jfinal/ext/kit/DateKit.java

@@ -26,40 +26,42 @@ import com.jfinal.kit.StrKit;
  */
 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) {
-		if (dateStr == null || "".equals(dateStr.trim())) {
+		if (StrKit.isBlank(dateStr)) {
 			return null;
 		}
+		
+		dateStr = dateStr.trim();
 		int length = dateStr.length();
 		try {
-			if (length == timeFormat.length()) {
-				SimpleDateFormat sdfDate = new SimpleDateFormat(timeFormat);
+			if (length == timeStampPattern.length()) {
+				SimpleDateFormat sdf = new SimpleDateFormat(timeStampPattern);
 				try {
-					return sdfDate.parse(dateStr);
+					return sdf.parse(dateStr);
 				} catch (ParseException e) {
 					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 {
 					return sdfDate.parse(dateStr);
 				} catch (ParseException e) {
@@ -68,19 +70,25 @@ public class DateKit {
 					return sdfDate.parse(dateStr);
 				}
 			} 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) {
-			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) {
-		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);
 	}
 }
+
+
+
+
+
+