View Javadoc
1   package com.gh.mygreen.xlsmapper.cellconverter.impl;
2   
3   import java.sql.Time;
4   import java.text.DateFormat;
5   import java.text.ParseException;
6   import java.util.Calendar;
7   import java.util.Date;
8   
9   import com.gh.mygreen.xlsmapper.Configuration;
10  import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
11  import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
12  
13  /**
14   * {@link Time} を処理する {@link CellConverter} を作成するためのファクトリクラス。
15   * @since 2.0
16   * @author T.TSUCHIE
17   *
18   */
19  public class SqlTimeCellConverterFactory extends AbstractDateCellConverterFactory<Time> {
20  
21      @Override
22      public SqlTimeCellConverter create(final FieldAccessor field, final Configuration config) {
23  
24          final SqlTimeCellConverter cellConverter = new SqlTimeCellConverter(field, config, this);
25          setupCellConverter(cellConverter, field, config);
26  
27          return cellConverter;
28      }
29  
30      /**
31       * {@inheritDoc}
32       * 基準となる日付をExcelの1900年1月0日=1899年12月31日として、パースする。
33       */
34      @Override
35      protected Time parseString(final DateFormat formatter, final String text) throws ParseException {
36          Date date = formatter.parse(text);
37          Calendar cal = Calendar.getInstance();
38          cal.setTime(date);
39          cal.set(1899, 12-1, 31);
40  
41          return convertTypeValue(cal.getTime());
42      }
43  
44      @Override
45      protected Time convertTypeValue(Date date) {
46          return new Time(date.getTime());
47      }
48  
49      @Override
50      protected String getDefaultJavaPattern() {
51          return "HH:mm:ss";
52      }
53  
54      @Override
55      protected String getDefaultExcelPattern() {
56          return "HH:mm:ss";
57      }
58  
59      public class SqlTimeCellConverter extends AbstractDateCellConverter<Time> {
60  
61          private final SqlTimeCellConverterFactory convererFactory;
62  
63          private SqlTimeCellConverter(final FieldAccessor field, final Configuration config,
64                  final SqlTimeCellConverterFactory convererFactory) {
65              super(field, config);
66              this.convererFactory = convererFactory;
67          }
68  
69          @Override
70          protected Time convertTypeValue(final Date value) {
71              return convererFactory.convertTypeValue(value);
72          }
73  
74      }
75  
76  }