com.databasesandlife.util.wicket.YearMonthDayConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
The newest version!
package com.databasesandlife.util.wicket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
import com.databasesandlife.util.*;
/**
* Allows Wicket fields to have models of type {@link YearMonthDay}.
*
* In HTML, use <input type="date">
. In Wicket, add the following to the application class:
*
* protected IConverterLocator newConverterLocator() {
* ConverterLocator locator =
* (ConverterLocator) super.newConverterLocator();
* locator.set(YearMonthDay.class, new YearMonthDayConverter());
* return locator;
* }
*
*
* @deprecated use {@link DateTextField} instead
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
@Deprecated
public class YearMonthDayConverter implements IConverter{
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
private SimpleDateFormat userFormat;
public YearMonthDayConverter() { userFormat = format; }
public YearMonthDayConverter(SimpleDateFormat format) { userFormat = format; }
@Override
public YearMonthDay convertToObject(String arg0, Locale arg1) {
if(arg0 == null) return null;
try {
return YearMonthDay.newForYYYYMMDD(format.format(userFormat.parse(arg0)));
} catch (ParseException e) {
throw new ConversionException(e).setResourceKey("invalid");
}
}
@Override
public String convertToString(YearMonthDay arg0, Locale arg1) {
if(arg0 == null) return null;
try {
return userFormat.format(format.parse(arg0.toYYYYMMDD()));
} catch (ParseException e) {
throw new ConversionException(e).setResourceKey("invalid");
}
}
}