Issue #1789 Move Calendar and Date parsing out of ConvertUtil and also fix date parsing.
Former-commit-id:51955a719a
[formerly36b3c16a42
] [formerly51955a719a
[formerly36b3c16a42
] [formerlyddf2092bde
[formerly 9491a90efdb389372e3ff3c988eb19aac9d508b6]]] Former-commit-id:ddf2092bde
Former-commit-id:a1ca2ae2d7
[formerly247c9f6c1f
] Former-commit-id:04882c798f
This commit is contained in:
parent
ca95cb0fce
commit
15a16c8d81
3 changed files with 47 additions and 58 deletions
|
@ -26,8 +26,9 @@ import java.util.TimeZone;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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
|
* Custom converter implementation for converting Calendar objects to and from
|
||||||
|
@ -38,6 +39,8 @@ import org.apache.commons.beanutils.Converter;
|
||||||
* Date Ticket# Engineer Description
|
* Date Ticket# Engineer Description
|
||||||
* ------------ ---------- ----------- --------------------------
|
* ------------ ---------- ----------- --------------------------
|
||||||
* bphillip Initial Creation
|
* bphillip Initial Creation
|
||||||
|
* Mar 13, 2013 1789 bsteffen Move Calendar and Date parsing out of
|
||||||
|
* ConvertUtil and also fix date parsing.
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author bphillip
|
* @author bphillip
|
||||||
|
@ -51,12 +54,18 @@ public class CalendarConverter implements Converter {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("rawtypes")
|
||||||
@Override
|
@Override
|
||||||
public Object convert(Class type, Object value) {
|
public Object convert(Class type, Object value) {
|
||||||
|
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
String date = (String) value;
|
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);
|
Matcher m = DATE_PATTERN.matcher(date);
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,8 @@ package com.raytheon.uf.common.time.util;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Queue;
|
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
import org.apache.commons.beanutils.Converter;
|
import org.apache.commons.beanutils.Converter;
|
||||||
|
|
||||||
|
@ -35,6 +35,8 @@ import org.apache.commons.beanutils.Converter;
|
||||||
* Date Ticket# Engineer Description
|
* Date Ticket# Engineer Description
|
||||||
* ------------ ---------- ----------- --------------------------
|
* ------------ ---------- ----------- --------------------------
|
||||||
* 9/17/08 1531 bphillip Initial Creation
|
* 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>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @author bphillip
|
* @author bphillip
|
||||||
|
@ -42,38 +44,48 @@ import org.apache.commons.beanutils.Converter;
|
||||||
*/
|
*/
|
||||||
public class DateConverter implements 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
|
@Override
|
||||||
public Object convert(Class clazz, Object value) {
|
public Object convert(Class clazz, Object value) {
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
SimpleDateFormat formatter = getDateFormat();
|
String valueString = (String) value;
|
||||||
try {
|
try {
|
||||||
return formatter.parseObject((String) value);
|
// see if string is in ISO 8601
|
||||||
} catch (ParseException e) {
|
return DatatypeConverter.parseDateTime(valueString).getTime();
|
||||||
e.printStackTrace();
|
} catch (Exception e) {
|
||||||
return null;
|
// try the formats.
|
||||||
} finally {
|
}
|
||||||
if (formatter != null && formatters.size() < QUEUE_SIZE) {
|
|
||||||
formatters.add(formatter);
|
for (SimpleDateFormat format : formatHolder.get()) {
|
||||||
|
try {
|
||||||
|
return format.parseObject(valueString);
|
||||||
|
} catch (ParseException e) {
|
||||||
|
// try the next one.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SimpleDateFormat getDateFormat() {
|
|
||||||
SimpleDateFormat m = formatters.poll();
|
|
||||||
if (m == null) {
|
|
||||||
m = new SimpleDateFormat(DATE_FORMAT);
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,14 +20,9 @@
|
||||||
package com.raytheon.uf.common.util;
|
package com.raytheon.uf.common.util;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
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.ConvertUtils;
|
||||||
import org.apache.commons.beanutils.Converter;
|
import org.apache.commons.beanutils.Converter;
|
||||||
import org.apache.commons.lang.time.DateUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utilities for converting objects, originally taken from
|
* Utilities for converting objects, originally taken from
|
||||||
|
@ -40,6 +35,8 @@ import org.apache.commons.lang.time.DateUtils;
|
||||||
* Date Ticket# Engineer Description
|
* Date Ticket# Engineer Description
|
||||||
* ------------ ---------- ----------- --------------------------
|
* ------------ ---------- ----------- --------------------------
|
||||||
* Mar 19, 2009 njensen Initial creation
|
* 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>
|
* </pre>
|
||||||
*
|
*
|
||||||
|
@ -90,35 +87,6 @@ public class ConvertUtil {
|
||||||
if (desiredClass.isEnum()) {
|
if (desiredClass.isEnum()) {
|
||||||
return Enum.valueOf((Class<? extends Enum>) desiredClass, value);
|
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);
|
return ConvertUtils.convert(value, desiredClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue