diff --git a/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java b/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java index 0839598..8d21d69 100644 --- a/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java +++ b/src/main/java/com/vitadairy/libraries/importexport/processor/WriteCellProcessor.java @@ -5,7 +5,6 @@ import lombok.RequiredArgsConstructor; import org.apache.poi.ss.usermodel.Cell; -import java.util.Date; import java.util.Objects; /** @@ -22,15 +21,12 @@ public void write(Cell cell, Object input) throws Exception { return; } switch (cellMetaData.getDataType()) { - case STRING: + case STRING, DATE: cell.setCellValue((String) cellData); break; case NUMBER: cell.setCellValue(((Number) cellData).doubleValue()); break; - case DATE: - cell.setCellValue((Date) cellData); - break; default: break; } diff --git a/src/main/java/com/vitadairy/libraries/importexport/service/WriteDataDateService.java b/src/main/java/com/vitadairy/libraries/importexport/service/WriteDataDateService.java index efbf9a3..9cc4911 100644 --- a/src/main/java/com/vitadairy/libraries/importexport/service/WriteDataDateService.java +++ b/src/main/java/com/vitadairy/libraries/importexport/service/WriteDataDateService.java @@ -3,7 +3,11 @@ import com.vitadairy.libraries.importexport.helper.ILogger; import com.vitadairy.libraries.importexport.utils.ReflectionUtils; +import java.sql.Timestamp; import java.text.SimpleDateFormat; +import java.time.Instant; +import java.util.Date; +import java.util.Objects; import java.util.Optional; /** @@ -37,12 +41,29 @@ public Object parseInputData(Object input, String fieldName) throws Exception { return null; } Object value = res.get(); - try { - return sdf.parse(value.toString()); - } catch (Exception e) { - logger.error("Error parsing date: " + value, e); - return null; + + if (value instanceof Date date) { + return sdf.format(date); + } + + if (value instanceof Instant instant) { + return sdf.format(Date.from(instant)); + } + + if (value instanceof Long longValue) { + return sdf.format(new Date(longValue)); } + + if (value instanceof Timestamp timestamp) { + return sdf.format(new Date(timestamp.getTime())); + } + + if (value instanceof String) { + Date date = sdf.parse(value.toString()); + return Objects.nonNull(date) ? sdf.format(date) : null; + } + + return null; } }