View Javadoc

1   /*
2    * Copyright 2004-2010 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  
17  package org.seasar.cubby.converter.impl;
18  
19  import java.util.Date;
20  
21  import org.seasar.cubby.converter.ConversionException;
22  import org.seasar.cubby.converter.ConversionHelper;
23  
24  /**
25   * 任意のオブジェクトから{@link Date}への変換を行うコンバータです。
26   * <p>
27   * 変換元のオブジェクトの文字列表現をフォーマットに従って{@link Date}に変換した結果を変換先とします。
28   * </p>
29   * 
30   * @author baba
31   */
32  public class DateConverter extends AbstractDateConverter {
33  
34  	/**
35  	 * {@inheritDoc}
36  	 */
37  	public Class<?> getObjectType() {
38  		return Date.class;
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	public Object convertToObject(final Object value,
45  			final Class<?> objectType, final ConversionHelper helper)
46  			throws ConversionException {
47  		if (value == null) {
48  			return null;
49  		}
50  		final String pattern = helper.getFormatPattern().getDatePattern();
51  		return toDate(value.toString(), pattern);
52  	}
53  
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	public String convertToString(final Object value,
58  			final ConversionHelper helper) {
59  		if (value == null) {
60  			return null;
61  		}
62  		final String pattern = helper.getFormatPattern().getDatePattern();
63  		return toString((Date) value, pattern);
64  	}
65  
66  }