Issue #1789 Move Calendar and Date parsing out of ConvertUtil and also fix date parsing.

Former-commit-id: 9491a90efdb389372e3ff3c988eb19aac9d508b6
This commit is contained in:
Ben Steffensmeier 2013-03-13 13:18:13 -05:00
parent 28d74d91b0
commit ddf2092bde
3 changed files with 47 additions and 58 deletions

View file

@ -26,8 +26,9 @@ import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.beanutils.Converter;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.beanutils.Converter;
/**
* Custom converter implementation for converting Calendar objects to and from
@ -38,6 +39,8 @@ import org.apache.commons.beanutils.Converter;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* bphillip Initial Creation
* Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of
* ConvertUtil and also fix date parsing.
* </pre>
*
* @author bphillip
@ -51,12 +54,18 @@ public class CalendarConverter implements Converter {
}
@SuppressWarnings("unchecked")
@SuppressWarnings("rawtypes")
@Override
public Object convert(Class type, Object value) {
if (value instanceof String) {
String date = (String) value;
try {
// see if string is in ISO 8601
return DatatypeConverter.parseDateTime(date).getTime();
} catch (Exception e) {
// try to match the pattern.
}
Matcher m = DATE_PATTERN.matcher(date);

View file

@ -22,8 +22,8 @@ package com.raytheon.uf.common.time.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.beanutils.Converter;
@ -35,6 +35,8 @@ import org.apache.commons.beanutils.Converter;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* 9/17/08 1531 bphillip Initial Creation
* Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of
* ConvertUtil and also fix date parsing.
* </pre>
*
* @author bphillip
@ -42,38 +44,48 @@ import org.apache.commons.beanutils.Converter;
*/
public class DateConverter implements Converter {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
// Allows ConvertUtils to successfully convert:
// 1) TimeRange.getStart().toString()
// 2) TimeRange.getEnd().toString()
// 3) "BinOffset usage"
private static final String[] DATE_FORMATS = { "yyyy-MM-dd HH:mm:ss.S",
"EEE MMM dd HH:mm:ss z yyyy", "yyyy-MM-dd HH:mm:ss" };
private static final int QUEUE_SIZE = 20;
private ThreadLocal<SimpleDateFormat[]> formatHolder = new ThreadLocal<SimpleDateFormat[]>() {
private static Queue<SimpleDateFormat> formatters = new ConcurrentLinkedQueue<SimpleDateFormat>();
@Override
protected SimpleDateFormat[] initialValue() {
SimpleDateFormat[] value = new SimpleDateFormat[DATE_FORMATS.length];
for (int i = 0; i < value.length; i += 1) {
value[i] = new SimpleDateFormat(DATE_FORMATS[i]);
}
return value;
}
};
@SuppressWarnings("rawtypes")
@Override
public Object convert(Class clazz, Object value) {
if (value instanceof String) {
SimpleDateFormat formatter = getDateFormat();
String valueString = (String) value;
try {
return formatter.parseObject((String) value);
} catch (ParseException e) {
e.printStackTrace();
return null;
} finally {
if (formatter != null && formatters.size() < QUEUE_SIZE) {
formatters.add(formatter);
// see if string is in ISO 8601
return DatatypeConverter.parseDateTime(valueString).getTime();
} catch (Exception e) {
// try the formats.
}
for (SimpleDateFormat format : formatHolder.get()) {
try {
return format.parseObject(valueString);
} catch (ParseException e) {
// try the next one.
}
}
return null;
}
return null;
}
private static SimpleDateFormat getDateFormat() {
SimpleDateFormat m = formatters.poll();
if (m == null) {
m = new SimpleDateFormat(DATE_FORMAT);
}
return m;
}
}

View file

@ -20,14 +20,9 @@
package com.raytheon.uf.common.util;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.lang.time.DateUtils;
/**
* Utilities for converting objects, originally taken from
@ -40,6 +35,8 @@ import org.apache.commons.lang.time.DateUtils;
* Date Ticket# Engineer Description
* ------------ ---------- ----------- --------------------------
* Mar 19, 2009 njensen Initial creation
* Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of
* ConvertUtil and also fix date parsing.
*
* </pre>
*
@ -90,35 +87,6 @@ public class ConvertUtil {
if (desiredClass.isEnum()) {
return Enum.valueOf((Class<? extends Enum>) desiredClass, value);
}
if (desiredClass.equals(Calendar.class)) {
try {
// see if string is in ISO 8601
return DatatypeConverter.parseDateTime(value);
} catch (Exception e) {
// let convertUtils try
}
}
if (desiredClass.equals(Date.class)) {
try {
// see if string is in ISO 8601
return DatatypeConverter.parseDateTime(value).getTime();
} catch (Exception e) {
// try with DateUtils, convertUtils is unlikely
// to succeed
}
// Allows ConvertUtils to successfully convert:
// 1) TimeRange.getStart().toString()
// 2) TimeRange.getEnd().toString()
// 3) "BinOffset usage"
final String[] parsePatterns = { "yyyy-MM-dd hh:mm:ss.S",
"EEE MMM dd hh:mm:ss z yyyy" };
try {
return DateUtils.parseDate(value, parsePatterns);
} catch (Exception e) {
// let convertUtils try
}
}
return ConvertUtils.convert(value, desiredClass);
}