]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/output/DateSelector.java
UPD: move the standard date format to "dateSelector"
[gigi.git] / src / org / cacert / gigi / output / DateSelector.java
index e4e66f6f4c4f92e316f2cf186f486e1830f45dff..0728eca3e73a7d69bbaf670e1779112fc344e603 100644 (file)
@@ -11,6 +11,7 @@ import java.util.TimeZone;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.localisation.Language;
 
 public class DateSelector implements Outputable {
@@ -38,6 +39,8 @@ public class DateSelector implements Outputable {
 
     private int year;
 
+    private static ThreadLocal<SimpleDateFormat> fmt = new ThreadLocal<>();
+
     @Override
     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
         out.print("<nobr>");
@@ -88,30 +91,25 @@ public class DateSelector implements Outputable {
         out.print("\" size=\"4\" autocomplete=\"off\">");
     }
 
-    public void update(HttpServletRequest r) {
-        String dayS = r.getParameter(names[0]);
-        if (dayS != null) {
-            day = parseIntSafe(dayS);
-        }
-
-        String monthS = r.getParameter(names[1]);
-        if (monthS != null) {
-            month = parseIntSafe(monthS);
-        }
+    public void update(HttpServletRequest r) throws GigiApiException {
+        try {
+            String dayS = r.getParameter(names[0]);
+            if (dayS != null) {
+                day = Integer.parseInt(dayS);
+            }
 
-        String yearS = r.getParameter(names[2]);
-        if (yearS != null) {
-            year = parseIntSafe(yearS);
-        }
-    }
+            String monthS = r.getParameter(names[1]);
+            if (monthS != null) {
+                month = Integer.parseInt(monthS);
+            }
 
-    private int parseIntSafe(String dayS) {
-        try {
-            return Integer.parseInt(dayS);
+            String yearS = r.getParameter(names[2]);
+            if (yearS != null) {
+                year = Integer.parseInt(yearS);
+            }
         } catch (NumberFormatException e) {
-
+            throw new GigiApiException("Unparsable date.");
         }
-        return 0;
     }
 
     public boolean isValid() {
@@ -132,4 +130,14 @@ public class DateSelector implements Outputable {
         return new java.sql.Date(gc.getTime().getTime());
     }
 
+    public static SimpleDateFormat getDateFormat() {
+        SimpleDateFormat local = fmt.get();
+        if (local == null) {
+            local = new SimpleDateFormat("yyyy-MM-dd");
+            local.setTimeZone(TimeZone.getTimeZone("UTC"));
+            fmt.set(local);
+        }
+        return local;
+    }
+
 }