All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.table.convert.TimestampStringConverter Maven / Gradle / Ivy

There is a newer version: 1.4.8
Show newest version
package com.litongjava.table.convert;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class TimestampStringConverter implements Converter {

  @Override
  public Class supportJavaTypeKey() {
    return Timestamp.class;
  }

  @Override
  public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
  }

  @Override
  public Timestamp convertToJavaData(@SuppressWarnings("rawtypes") CellData cellData,
      ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
    String value = cellData.getStringValue();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return new Timestamp(formatter.parse(value).getTime());
  }

  @Override
  public CellData convertToExcelData(Timestamp value, ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) throws Exception {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String string = formatter.format(value);
    return new CellData<>(string);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy