Skip to content

Commit

Permalink
refs #25 日付型にマッピングする際に標準の書式を設定するよう修正。
Browse files Browse the repository at this point in the history
  • Loading branch information
mygreen committed May 5, 2015
1 parent 8002ddd commit bf4b094
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
/**
* 日時型のConverterの抽象クラス。
* <p>{@link Date}を継承している<code>javax.sql.Time/Date/Timestamp</code>はこのクラスを継承して作成します。
*
*
* @version 0.5
* @author T.TSUCHIE
*
*/
public abstract class AbstractDateCellConverter<T extends Date> extends AbstractCellConverter<T> {

public static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

@Override
public T toObject(final Cell cell, final FieldAdaptor adaptor, final XlsMapperConfig config) throws XlsMapperException {

Expand All @@ -53,7 +52,7 @@ public T toObject(final Cell cell, final FieldAdaptor adaptor, final XlsMapperCo
try {
resultValue = parseDate(defaultValue, createDateFormat(anno));
} catch(ParseException e) {
throw newTypeBindException(cell, adaptor, defaultValue)
throw newTypeBindException(e, cell, adaptor, defaultValue)
.addAllMessageVars(createTypeErrorMessageVars(anno));
}
}
Expand All @@ -69,7 +68,7 @@ public T toObject(final Cell cell, final FieldAdaptor adaptor, final XlsMapperCo
try {
resultValue = parseDate(cellValue, createDateFormat(anno));
} catch(ParseException e) {
throw newTypeBindException(cell, adaptor, cellValue)
throw newTypeBindException(e, cell, adaptor, cellValue)
.addAllMessageVars(createTypeErrorMessageVars(anno));
}
}
Expand All @@ -91,20 +90,15 @@ public T toObject(final Cell cell, final FieldAdaptor adaptor, final XlsMapperCo
*/
protected DateFormat createDateFormat(final XlsDateConverter anno) throws AnnotationInvalidException {

if(anno.pattern().isEmpty()) {
throw new AnnotationInvalidException(
String.format("Anotation '@%s' attribute 'pattern' should be not empty.", XlsDateConverter.class.getSigners()),
anno);
}

final Locale locale;
if(anno.locale().isEmpty()) {
locale = Locale.getDefault();
} else {
locale = Utils.getLocale(anno.locale());
}

final DateFormat format = new SimpleDateFormat(anno.pattern(), locale);
final String pattern = anno.pattern().isEmpty() ? getDefaultPattern() : anno.pattern();
final DateFormat format = new SimpleDateFormat(pattern, locale);
format.setLenient(anno.lenient());

return format;
Expand All @@ -130,6 +124,13 @@ private Map<String, Object> createTypeErrorMessageVars(final XlsDateConverter an
*/
abstract protected T convertDate(final Date value);

/**
* その型における標準の書式を返す。
* @since 0.5
* @return {@link SimpleDateFormat}で処理可能な形式。
*/
abstract protected String getDefaultPattern();

/**
* 文字列をその型における日付型を返す。
* <p>アノテーション{@link XlsDateConverter}でフォーマットが与えられている場合は、パースして返す。
Expand All @@ -152,7 +153,8 @@ public Class<? extends Annotation> annotationType() {

@Override
public String pattern() {
return "";
// 各タイプごとの標準の書式を取得する。
return getDefaultPattern();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

/**
* {@link Date}型を処理するConverter.
*
* <p>標準の書式として{@code yyyy-MM-dd HH:mm:ss}で処理する。
*
* @version 0.5
* @author T.TSUCHIE
*
*/
Expand All @@ -15,4 +17,9 @@ public class DateCellConverter extends AbstractDateCellConverter<Date> {
protected Date convertDate(Date value) {
return value;
}

@Override
protected String getDefaultPattern() {
return "yyyy-MM-dd HH:mm:ss";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@

/**
* {@link java.sql.Date}型を処理するためのConverter.
*
* <p>標準の書式として{@code yyyy-MM-dd}で処理する。
*
* @version 0.5
* @author T.TSUCHIE
*
*/
public class SqlDateCellConverter extends AbstractDateCellConverter<java.sql.Date> {

@Override
protected java.sql.Date convertDate(Date value) {
return new java.sql.Date(value.getTime());
}

@Override
protected String getDefaultPattern() {
return "yyyy-MM-dd";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

/**
* {@link java.sql.Time}型を処理するためのConverter.
* <p>標準の書式として{@code HH:mm:ss}で処理する。
*
* @version 0.5
* @author T.TSUCHIE
*
*/
Expand All @@ -18,6 +20,9 @@ protected Time convertDate(Date value) {
return new Time(value.getTime());
}


@Override
protected String getDefaultPattern() {
return "HH:mm:ss";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@

/**
* {@link java.sql.Timestamp}型を処理するためのConverter.
*
* <p>標準の書式として{@code HH:mm:ss.SSS}で処理する。
*
* @version 0.5
* @author T.TSUCHIE
*
*/
public class SqlTimestampCellConverter extends AbstractDateCellConverter<Timestamp> {

@Override
protected Timestamp convertDate(Date value) {

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


@Override
protected String getDefaultPattern() {
return "yyyy-MM-dd HH:mm:ss.SSS";
}

}

0 comments on commit bf4b094

Please sign in to comment.