Java tutorial
/* * Copyright 2013 Christian Robert * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.perdian.commons.lang.conversion.impl.converters; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import org.springframework.context.i18n.LocaleContext; import de.perdian.commons.lang.conversion.ConverterException; public class StringToDateConverter extends AbstractDateFormatConverter<String, Date> { private Class<? extends Date> targetDateClass = Date.class; public StringToDateConverter() { } public StringToDateConverter(DateFormat dateFormat) { super(dateFormat); } public StringToDateConverter(String pattern) { super(pattern); } public StringToDateConverter(String pattern, LocaleContext localeContext) { super(pattern, localeContext); } @Override protected Date convertUsingFormat(DateFormat dateFormat, String source) { Date utilDateValue = this.parseUsingFormat(dateFormat, source); Class<? extends Date> targetDateClass = this.getTargetDateClass(); if (targetDateClass == null || targetDateClass.equals(java.util.Date.class)) { return utilDateValue; } else if (targetDateClass.equals(java.sql.Date.class)) { return new java.sql.Date(utilDateValue.getTime()); } else if (targetDateClass.equals(java.sql.Timestamp.class)) { return new java.sql.Timestamp(utilDateValue.getTime()); } else if (targetDateClass.equals(java.sql.Time.class)) { return new java.sql.Time(utilDateValue.getTime()); } else { throw new IllegalArgumentException( "Cannot convert date into target object of class: " + targetDateClass.getName()); } } protected Date parseUsingFormat(DateFormat dateFormat, String source) { try { return source == null ? null : dateFormat.parse(source); } catch (ParseException e) { StringBuilder errorMessage = new StringBuilder(); errorMessage.append("Cannot parse value '").append(source).append("' into "); errorMessage.append(" valid Date using format '").append(dateFormat); errorMessage.append("'"); throw new ConverterException(source, errorMessage.toString()); } } @Override protected DateFormat resolveDefaultFormat(Locale locale) { String pattern = this.getPattern(); if (pattern != null) { return new SimpleDateFormat(pattern, locale); } else { Class<? extends Date> targetDateClass = this.getTargetDateClass(); if (targetDateClass == null || targetDateClass.equals(java.util.Date.class) || targetDateClass.equals(java.sql.Date.class)) { return DateFormat.getDateInstance(DateFormat.DEFAULT, locale); } else if (targetDateClass.equals(java.sql.Timestamp.class)) { return DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale); } else if (targetDateClass.equals(java.sql.Time.class)) { return DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); } else { return DateFormat.getInstance(); } } } // ------------------------------------------------------------------------- // --- Property access methods --------------------------------------------- // ------------------------------------------------------------------------- public Class<? extends Date> getTargetDateClass() { return this.targetDateClass; } public void setTargetDateClass(Class<? extends Date> targetDateClass) { this.targetDateClass = targetDateClass; } }