
org.skyway.spring.util.databinding.StringToCalendar Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2007 - 2011 Skyway Software, Inc.
*/
package org.skyway.spring.util.databinding;
import java.util.Calendar;
import java.util.Locale;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.springframework.binding.convert.converters.StringToObject;
public class StringToCalendar extends StringToObject {
private CalendarConverter converter = null;
/**
* Creates a CustomCalendarConverter with a default format of date time with timezone in ISO
* format
*
* @see DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT
*/
public StringToCalendar() {
this(CalendarConverter.DEFAULT.getPattern());
}
/**
* Returns a CustomCalendarConverter with the pattern specified and the default Locale
*
* @param pattern
*/
public StringToCalendar(String pattern) {
this(pattern, null);
}
/**
* Returns a CustomCalendarConverter with the Locale specified, and the defult short Date Format
*
* @param locale
*/
public StringToCalendar(Locale locale) {
this(FastDateFormat.getDateInstance(FastDateFormat.SHORT, locale).getPattern(), locale);
}
/**
* Creates a CustomCalendarConverter using the pattern and Locale passed The pattern will be used
* without modification as one of the parsing patterns The Locale will be used to generate a set
* of other patterns that may also be used to parse If no Locale is passed, the default Locale
* is used
*
* @param dateFormat
* @param allowEmpty
*/
public StringToCalendar(String pattern, Locale locale) {
super(Calendar.class);
converter = new CalendarConverter(pattern, locale);
}
@SuppressWarnings("unchecked")
@Override
protected Object toObject(String text, Class arg1) throws Exception {
return converter.getCalendarFromText(text);
}
@Override
protected String toString(Object o) throws Exception {
Calendar calendar = (Calendar)o;
return converter.getTextFromCalendar((Calendar)calendar);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy