Coverage Report - org.seasar.cubby.converter.impl.AbstractDateConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDateConverter
92%
13/14
75%
6/8
4.5
 
 1  
 /*
 2  
  * Copyright 2004-2009 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  
 package org.seasar.cubby.converter.impl;
 17  
 
 18  
 import java.text.DateFormat;
 19  
 import java.text.ParseException;
 20  
 import java.text.ParsePosition;
 21  
 import java.text.SimpleDateFormat;
 22  
 import java.util.Date;
 23  
 
 24  
 import org.seasar.cubby.action.MessageInfo;
 25  
 import org.seasar.cubby.converter.ConversionException;
 26  
 
 27  
 /**
 28  
  * 文字列を日付型のオブジェクトに変換するクラスの抽象クラスです。
 29  
  * 
 30  
  * @author baba
 31  
  */
 32  76
 public abstract class AbstractDateConverter extends AbstractConverter {
 33  
 
 34  
         /**
 35  
          * 文字列を{@link Date}に変換して返します。
 36  
          * 
 37  
          * @param value
 38  
          *            変換元のオブジェクトの文字列表現
 39  
          * @param pattern
 40  
          *            日付と時刻のフォーマットを記述するパターン
 41  
          * @return 変換した結果の{@link Date}
 42  
          * @throws ConversionException
 43  
          *             {@link ParseException} が発生した場合
 44  
          * @see SimpleDateFormat#parse(String, ParsePosition)
 45  
          */
 46  
         protected Date toDate(final String value, final String pattern)
 47  
                         throws ConversionException {
 48  25
                 if (value == null || value.length() == 0) {
 49  0
                         return null;
 50  
                 }
 51  
 
 52  25
                 final ParsePosition parsePosition = new ParsePosition(0);
 53  25
                 final DateFormat dateFormat = new SimpleDateFormat(pattern);
 54  25
                 dateFormat.setLenient(false);
 55  25
                 final Date date = dateFormat.parse(value, parsePosition);
 56  25
                 if (date != null && parsePosition.getIndex() == value.length()) {
 57  17
                         return date;
 58  
                 }
 59  
 
 60  8
                 final MessageInfo messageInfo = new MessageInfo();
 61  8
                 messageInfo.setKey("valid.dateFormat");
 62  8
                 throw new ConversionException(messageInfo);
 63  
         }
 64  
 
 65  
         /**
 66  
          * 指定された日付を文字列に変換します。
 67  
          * 
 68  
          * @param date
 69  
          *            日付
 70  
          * @param pattern
 71  
          *            日付と時刻のフォーマットを記述するパターン
 72  
          * @return 指定された日付をフォーマットした文字列
 73  
          */
 74  
         protected String toString(final Date date, final String pattern) {
 75  1
                 final DateFormat dateFormat = new SimpleDateFormat(pattern);
 76  1
                 return dateFormat.format(date);
 77  
         }
 78  
 
 79  
 }