Skip to content

Commit

Permalink
feat - VTD - fix export date
Browse files Browse the repository at this point in the history
  • Loading branch information
duyenthaind committed Sep 9, 2024
1 parent 246499f commit e36d4e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Cell;

import java.util.Date;
import java.util.Objects;

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
}

}

0 comments on commit e36d4e6

Please sign in to comment.