1 package org.cacert.gigi.output;
3 import java.io.PrintWriter;
4 import java.text.SimpleDateFormat;
5 import java.util.Arrays;
6 import java.util.Calendar;
8 import java.util.GregorianCalendar;
10 import java.util.TimeZone;
12 import javax.servlet.http.HttpServletRequest;
14 import org.cacert.gigi.GigiApiException;
15 import org.cacert.gigi.localisation.Language;
17 public class DateSelector implements Outputable {
19 private String[] names;
21 public DateSelector(String day, String month, String year, Date date) {
22 this(day, month, year);
23 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTF"));
25 this.day = cal.get(Calendar.DAY_OF_MONTH);
26 this.month = cal.get(Calendar.MONTH);
27 this.year = cal.get(Calendar.YEAR);
30 public DateSelector(String day, String month, String year) {
31 this.names = new String[] {
42 private static ThreadLocal<SimpleDateFormat> fmt = new ThreadLocal<>();
45 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
53 private void outputDay(PrintWriter out) {
54 out.print("<select name=\"");
57 for (int i = 1; i <= 31; i++) {
60 out.print(" selected=\"selected\"");
62 out.println(">" + i + "</option>");
64 out.println("</select>");
67 private void outputMonth(PrintWriter out, Language l) {
68 SimpleDateFormat sdf = new SimpleDateFormat("MMMM", l.getLocale());
69 out.print("<select name=\"");
72 Calendar c = sdf.getCalendar();
73 for (int i = 1; i <= 12; i++) {
74 c.set(Calendar.MONTH, i - 1);
75 out.print("<option value='" + i + "'");
77 out.print(" selected=\"selected\"");
79 out.println(">" + sdf.format(c.getTime()) + " (" + i + ")</option>");
81 out.println("</select>");
84 private void outputYear(PrintWriter out) {
85 out.print("<input type=\"text\" name=\"");
87 out.print("\" value=\"");
91 out.print("\" size=\"4\" autocomplete=\"off\">");
94 public void update(HttpServletRequest r) throws GigiApiException {
96 String dayS = r.getParameter(names[0]);
98 day = Integer.parseInt(dayS);
101 String monthS = r.getParameter(names[1]);
102 if (monthS != null) {
103 month = Integer.parseInt(monthS);
106 String yearS = r.getParameter(names[2]);
108 year = Integer.parseInt(yearS);
110 } catch (NumberFormatException e) {
111 throw new GigiApiException("Unparsable date.");
115 public boolean isValid() {
116 if ( !(1900 < year && 1 <= month && month <= 12 && 1 <= day && day <= 32)) {
119 return true; // TODO checkdate
123 public String toString() {
124 return "DateSelector [names=" + Arrays.toString(names) + ", day=" + day + ", month=" + month + ", year=" + year + "]";
127 public java.sql.Date getDate() {
128 Calendar gc = GregorianCalendar.getInstance();
129 gc.set(year, month - 1, day);
130 return new java.sql.Date(gc.getTime().getTime());
133 public static SimpleDateFormat getDateFormat() {
134 SimpleDateFormat local = fmt.get();
136 local = new SimpleDateFormat("yyyy-MM-dd");
137 local.setLenient(false);
138 local.setTimeZone(TimeZone.getTimeZone("UTC"));