package com.exedio.copernica;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.exedio.cope.util.Day;

final class DayField extends TextField
{
	private static final String DATE_FORMAT_FULL = "dd.MM.yyyy";

	final Day content;
	final String pattern;

	/**
	 * Constructs a form field with an initial value.
	 */
	public DayField(final Form form, final Object key, final String name, final Day value)
	{
		this(DATE_FORMAT_FULL, form, key, name, value);
	}

	/**
	 * Constructs a form field with an initial value.
	 */
	public DayField(final String pattern, final Form form, final Object key, final String name, final Day value)
	{
		super(form, key, name, (value==null) ? "" : (new SimpleDateFormat(pattern)).format(new Date(value.getTimeInMillisFrom())));

		this.pattern = pattern;
		this.content = value;

		if(pattern==null)
			throw new NullPointerException("pattern");
	}

	/**
	 * Constructs a form field with a value obtained from the submitted form.
	 */
	public DayField(final Form form, final Object key, final String name)
	{
		this(DATE_FORMAT_FULL, form, key, name);
	}

	/**
	 * Constructs a form field with a value obtained from the submitted form.
	 */
	public DayField(final String pattern, final Form form, final Object key, final String name)
	{
		super(form, key, name);

		this.pattern = pattern;

		final String value = this.value;
		if(value.length()>0)
		{
			Day parsed = null;
			try
			{
				final SimpleDateFormat df = new SimpleDateFormat(pattern);
				parsed = new Day(df.parse(value));
			}
			catch(ParseException e)
			{
				error = "bad date: "+e.getMessage();
			}
			content = error==null ? parsed : null;
		}
		else
			content = null;
	}

	@Override
	public Object getContent()
	{
		return content;
	}

}

@@ -114,16 +114,43 @@
 il.Date;
+%0Aimport java.util.TimeZone;
 %0A%0Aimport
@@ -175,16 +175,60 @@
 til.Day;
+%0Aimport com.exedio.cope.util.TimeZoneStrict;
 %0A%0Afinal 
@@ -323,16 +323,99 @@
 M.yyyy%22;
+%0A%09private static final TimeZone ZONE = TimeZoneStrict.getTimeZone(%22Europe/Berlin%22);
 %0A%0A%09final
@@ -952,16 +952,20 @@
 lisFrom(
+ZONE
 ))));%0A%0A%09
@@ -1684,16 +1684,16 @@
 ttern);%0A
-
 %09%09%09%09pars
@@ -1720,16 +1720,22 @@
 e(value)
+, ZONE
 );%0A%09%09%09%7D%0A

fix deprecation of Day without TimeZone