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.TimeZone;
10 import javax.servlet.http.HttpServletRequest;
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Outputable;
15 import org.cacert.gigi.util.CalendarUtil;
16 import org.cacert.gigi.util.DayDate;
17 import org.cacert.gigi.util.HTMLEncoder;
19 public class DateSelector implements Outputable {
21 private String[] names;
23 public DateSelector(String day, String month, String year, DayDate date) {
24 this(day, month, year);
25 Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
26 cal.setTimeInMillis(date.getTime());
27 this.day = cal.get(Calendar.DAY_OF_MONTH);
28 this.month = cal.get(Calendar.MONTH) + 1;
29 this.year = cal.get(Calendar.YEAR);
32 public DateSelector(String day, String month, String year) {
33 this.names = new String[] {
34 HTMLEncoder.encodeHTML(day), HTMLEncoder.encodeHTML(month), HTMLEncoder.encodeHTML(year)
44 private static ThreadLocal<SimpleDateFormat> fmt = new ThreadLocal<>();
47 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
55 private void outputDay(PrintWriter out) {
56 out.print("<select name=\"");
59 for (int i = 1; i <= 31; i++) {
62 out.print(" selected=\"selected\"");
64 out.println(">" + i + "</option>");
66 out.println("</select>");
69 private void outputMonth(PrintWriter out, Language l) {
70 SimpleDateFormat sdf = new SimpleDateFormat("MMMM", l.getLocale());
71 out.print("<select name=\"");
74 Calendar c = sdf.getCalendar();
75 for (int i = 1; i <= 12; i++) {
76 c.set(Calendar.MONTH, i - 1);
77 out.print("<option value='" + i + "'");
79 out.print(" selected=\"selected\"");
81 out.println(">" + sdf.format(c.getTime()) + " (" + i + ")</option>");
83 out.println("</select>");
86 private void outputYear(PrintWriter out) {
87 out.print("<input type=\"text\" name=\"");
89 out.print("\" value=\"");
93 out.println("\" size=\"4\" autocomplete=\"off\">");
96 public void update(HttpServletRequest r) throws GigiApiException {
98 String dayS = r.getParameter(names[0]);
100 day = Integer.parseInt(dayS);
103 String monthS = r.getParameter(names[1]);
104 if (monthS != null) {
105 month = Integer.parseInt(monthS);
108 String yearS = r.getParameter(names[2]);
110 year = Integer.parseInt(yearS);
112 } catch (NumberFormatException e) {
113 throw new GigiApiException("Unparsable date.");
117 public boolean isValid() {
118 if ( !(1890 < year && 1 <= month && month <= 12 && 1 <= day && day <= 32)) {
122 if ( !CalendarUtil.isDateValid(year, month, day)) {
130 public String toString() {
131 return "DateSelector [names=" + Arrays.toString(names) + ", day=" + day + ", month=" + month + ", year=" + year + "]";
134 public DayDate getDate() {
135 return CalendarUtil.getDateFromComponents(year, month, day);
138 public static SimpleDateFormat getDateFormat() {
139 SimpleDateFormat local = fmt.get();
141 local = new SimpleDateFormat("yyyy-MM-dd");
142 local.setLenient(false);
143 local.setTimeZone(TimeZone.getTimeZone("UTC"));