com.soento.core.util.DateConverter Maven / Gradle / Ivy
package com.soento.core.util;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.converters.DateTimeConverter;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
/**
* @author soento
*/
public class DateConverter extends DateTimeConverter {
public DateConverter() {
}
public DateConverter(Object defaultValue) {
super(defaultValue);
}
@Override
protected String convertToString(Object value) throws Throwable {
if (value == null) {
return null;
}
java.util.Date date = null;
if ((value instanceof java.util.Date)) {
date = (java.util.Date) value;
} else if ((value instanceof Calendar)) {
date = ((Calendar) value).getTime();
} else if ((value instanceof Long)) {
date = new java.util.Date(((Long) value).longValue());
}
if (date == null) {
date = DateUtil.toDate(value.toString(), DateUtil.getDateFormat(value.toString()));
}
return DateUtil.toString(date);
}
@Override
protected Object convertToType(Class targetType, Object value) throws Exception {
Class sourceType = value.getClass();
if (value instanceof java.sql.Timestamp) {
// ---------------------- JDK 1.3 Fix ----------------------
// N.B. Prior to JDK 1.4 the Timestamp's getTime() method
// didn't include the milliseconds. The following code
// ensures it works consistently accross JDK versions
java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
long timeInMillis = ((timestamp.getTime() / 1000) * 1000);
timeInMillis += timestamp.getNanos() / 1000000;
// ---------------------- JDK 1.3 Fix ----------------------
return toDate(targetType, timeInMillis);
}
// Handle Date (includes java.sql.Date & java.sql.Time)
if (value instanceof Date) {
Date date = (Date) value;
return toDate(targetType, date.getTime());
}
// Handle Calendar
if (value instanceof Calendar) {
Calendar calendar = (Calendar) value;
return toDate(targetType, calendar.getTime().getTime());
}
// Handle Long
if (value instanceof Long) {
Long longObj = (Long) value;
return toDate(targetType, longObj.longValue());
}
// Convert all other types to String & handle
String stringValue = value.toString().trim();
if (stringValue.length() == 0) {
return handleMissing(targetType);
}
// Default String conversion
return toDate(targetType, stringValue);
}
@Override
protected Class> getDefaultType() {
return java.util.Date.class;
}
private Object toDate(Class type, long value) {
// java.util.Date
if (type.equals(Date.class)) {
return new Date(value);
}
// java.sql.Date
if (type.equals(java.sql.Date.class)) {
return new java.sql.Date(value);
}
// java.sql.Time
if (type.equals(Time.class)) {
return new Time(value);
}
// java.sql.Timestamp
if (type.equals(Timestamp.class)) {
return new Timestamp(value);
}
// java.util.Calendar
if (type.equals(Calendar.class)) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(value));
calendar.setLenient(false);
return calendar;
}
// java.util.Calendar
if (type.equals(String.class)) {
return DateUtil.toString(new Date(value));
}
String msg = getClass().getName() + " cannot handle conversion to '" + type.getName() + "'";
throw new ConversionException(msg);
}
private Object toDate(Class type, String value) {
// java.util.Date
if (type.equals(java.util.Date.class)) {
return DateUtil.toDate(value);
}
// java.sql.Date
if (type.equals(java.sql.Date.class)) {
try {
return java.sql.Date.valueOf(value);
} catch (IllegalArgumentException e) {
throw new ConversionException(
"String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date");
}
}
// java.sql.Time
if (type.equals(java.sql.Time.class)) {
try {
return java.sql.Time.valueOf(value);
} catch (IllegalArgumentException e) {
throw new ConversionException(
"String must be in JDBC format [HH:mm:ss] to create a java.sql.Time");
}
}
// java.sql.Timestamp
if (type.equals(java.sql.Timestamp.class)) {
try {
return java.sql.Timestamp.valueOf(value);
} catch (IllegalArgumentException e) {
throw new ConversionException(
"String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] " +
"to create a java.sql.Timestamp");
}
}
// java.sql.Timestamp
if (type.equals(java.sql.Timestamp.class)) {
try {
return java.sql.Timestamp.valueOf(value);
} catch (IllegalArgumentException e) {
throw new ConversionException(
"String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] " +
"to create a java.sql.Timestamp");
}
}
String msg = getClass().getName() + " does not support default String to '" + type.getName() + "' conversion.";
throw new ConversionException(msg);
}
}