LocalDateCellConverterFactory.java

  1. package com.gh.mygreen.xlsmapper.cellconverter.impl;

  2. import java.time.LocalDate;
  3. import java.time.LocalDateTime;
  4. import java.time.ZoneId;
  5. import java.time.ZonedDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.time.format.DateTimeParseException;
  8. import java.util.Date;

  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.  * {@link LocalDate}を処理する{@link CellConverter}を作成するためのファクトリクラス。
  14.  *
  15.  * @since 2.0
  16.  * @author T.TSUCHIE
  17.  *
  18.  */
  19. public class LocalDateCellConverterFactory extends AbstractTemporalCellConverterFactory<LocalDate>{

  20.     @Override
  21.     public LocalDateCellConverter create(final FieldAccessor field, final Configuration config) {
  22.        
  23.         final LocalDateCellConverter cellConverter = new LocalDateCellConverter(field, config);
  24.         setupCellConverter(cellConverter, field, config);
  25.        
  26.         return cellConverter;
  27.        
  28.     }
  29.    
  30.     @Override
  31.     protected LocalDate parseTemporal(final String str, final DateTimeFormatter formatter) throws DateTimeParseException {
  32.         return LocalDate.parse(str, formatter);
  33.     }
  34.    
  35.     @Override
  36.     protected String getDefaultJavaPattern() {
  37.         return "uuuu-MM-dd";
  38.     }

  39.     @Override
  40.     protected String getDefaultExcelPattern() {
  41.         return "yyyy-mm-dd";
  42.     }
  43.    
  44.     public class LocalDateCellConverter extends AbstractTemporalCellConverter<LocalDate> {
  45.        
  46.         private LocalDateCellConverter(final FieldAccessor field, final Configuration config) {
  47.             super(field, config);
  48.         }
  49.        
  50.         @Override
  51.         protected LocalDate convertFromDate(final Date date) {
  52.             LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
  53.             return dateTime.toLocalDate();
  54.         }
  55.        
  56.         @Override
  57.         protected Date convertToDate(final LocalDate value, final boolean dateStart1904) {
  58.             LocalDateTime dateTime = value.atStartOfDay();
  59.             ZonedDateTime zoneDateTime = ZonedDateTime.of(dateTime, ZoneId.systemDefault());
  60.             return Date.from(zoneDateTime.toInstant());
  61.         }
  62.        
  63.     }
  64.    
  65. }