date to sql timestamp converter for Oracle

My database is Oracle. When I use PopupDateField to show date, I get the exception the following exception:

Unable to convert value of type java.util.Date to model type class java.sql.Timestamp. No converter is set and the types are not compatible.

Then I learn its property type of this field is java.sql.timestamp. However, I can not find DateToSqlTimestampConverter. So I follow the code of DateToSqlDateConverter to create it:


package com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;
import com.vaadin.data.util.converter.Converter;

@SuppressWarnings("serial")
public class DateToSqlTimestampConverter implements Converter<Date, java.sql.Timestamp> {

	@Override
	public java.sql.Timestamp convertToModel(Date value, Class<? extends java.sql.Timestamp> targetType, Locale locale) throws ConversionException {
		if (targetType != getModelType()) {
			throw new ConversionException("Converter only supports " + getModelType().getName() + " (targetType was " + targetType.getName() + ")");
		}

		if (null == value)
			return null;

		return new java.sql.Timestamp(value.getTime());
	}

	@Override
	public Date convertToPresentation(java.sql.Timestamp value, Class<? extends Date> targetType, Locale locale) throws ConversionException {
		if (targetType != getPresentationType()) {
			throw new ConversionException("Converter only supports " + getPresentationType().getName() + " (targetType was " + targetType.getName() + ")");
		}

		if (null == value)
			return null;

		return new Date(value.getTime());
	}

	@Override
	public Class<java.sql.Timestamp> getModelType() {
		return java.sql.Timestamp.class;
	}

	@Override
	public Class<Date> getPresentationType() {
		return Date.class;
	}
}

This code works. Still I wonder If there is any other authentic way to achieve it.

This is the correct way. However, it may be better if you extend CustomField to create a Timestamp Field for better code reusability.