org.milyn.javabean.decoders.CalendarDecoder Maven / Gradle / Ivy
package org.milyn.javabean.decoders;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.milyn.javabean.DataDecodeException;
import org.milyn.javabean.DataDecoder;
import org.milyn.javabean.DecodeType;
/**
* {@link java.util.Calendar} data decoder.
*
* Decodes the supplied string into a {@link java.util.Calendar} value
* based on the supplied "{@link java.text.SimpleDateFormat format}" parameter.
*
* This decoder is synchronized on its underlying {@link SimpleDateFormat} instance.
*
* @see {@link LocaleAwareDateDecoder}
*
* @author [email protected]
* @author Pavel Kadlec
* @author [email protected]
*
*/
@DecodeType(Calendar.class)
public class CalendarDecoder extends LocaleAwareDateDecoder implements DataDecoder {
public Object decode(String data) throws DataDecodeException {
if (decoder == null) {
throw new IllegalStateException("Calendar decoder not initialised. A decoder for this type (" + getClass().getName() + ") must be explicitly configured (unlike the primitive type decoders) with a date 'format'. See Javadoc.");
}
try {
// Must be sync'd - DateFormat is not synchronized.
synchronized(decoder) {
decoder.parse(data.trim());
return decoder.getCalendar().clone();
}
} catch (ParseException e) {
throw new DataDecodeException("Error decoding Date data value '" + data + "' with decode format '" + format + "'.", e);
}
}
}