Skip to content

Commit 96c7c95

Browse files
feat: add formatter for the date
hale: add new Excel reader parameter that allows to specify import format for date cells At the moment the formatter has the following format: yyyy-MM-dd, but should be customizable by the user with a new issue. ING-4151
1 parent ec50283 commit 96c7c95

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

io/plugins/eu.esdihumboldt.hale.io.xls.test/src/eu/esdihumboldt/hale/io/xls/test/XLSInstanceWriterTestExamples.groovy

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
package eu.esdihumboldt.hale.io.xls.test
1717

18+
import java.text.SimpleDateFormat
19+
import java.time.LocalDate
20+
1821
import eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder
1922
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection
2023
import eu.esdihumboldt.hale.common.schema.groovy.SchemaBuilder
@@ -26,6 +29,11 @@ class XLSInstanceWriterTestExamples {
2629

2730
Schema schema = createSchema()
2831

32+
// Declare a date in the "dd/mm/yyyy" format
33+
def dateString1 = "25/12/2023"
34+
def dateFormat1 = new SimpleDateFormat("dd/MM/yyyy")
35+
def date1 = dateFormat1.parse(dateString1)
36+
2937
// create the instance collection
3038
// concrete types are only strings, since the test is not able to choose the correct type in wizard
3139
InstanceCollection instances = new InstanceBuilder(types: schema).createCollection {
@@ -59,6 +67,7 @@ class XLSInstanceWriterTestExamples {
5967
name('other')
6068
number('1')
6169
description('other type')
70+
date(date1)
6271
}
6372
}
6473
}
@@ -91,9 +100,9 @@ class XLSInstanceWriterTestExamples {
91100
name(String)
92101
number(String)
93102
description(String)
103+
date(LocalDate)
94104
}
95105
}
96106
return schema;
97107
}
98-
99108
}

io/plugins/eu.esdihumboldt.hale.io.xls/src/eu/esdihumboldt/hale/io/xls/XLSUtil.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515

1616
package eu.esdihumboldt.hale.io.xls;
1717

18+
import java.time.LocalDateTime;
19+
import java.time.ZoneId;
20+
import java.time.format.DateTimeFormatter;
21+
import java.util.Date;
22+
1823
import org.apache.poi.ss.usermodel.Cell;
1924
import org.apache.poi.ss.usermodel.CellType;
2025
import org.apache.poi.ss.usermodel.CellValue;
26+
import org.apache.poi.ss.usermodel.DateUtil;
2127
import org.apache.poi.ss.usermodel.FormulaEvaluator;
2228
import org.apache.poi.ss.usermodel.Row;
2329
import org.apache.poi.ss.usermodel.Sheet;
@@ -30,6 +36,12 @@
3036
*/
3137
public class XLSUtil {
3238

39+
/**
40+
* Parameter for the reader specifying how values imported from date cells
41+
* should be formatted.
42+
*/
43+
public static final String PARAMETER_DATE_FORMAT = "yyyy-MM-dd";
44+
3345
/**
3446
* Extract the text from a given cell. Formulas are evaluated, for blank or
3547
* error cells <code>null</code> is returned
@@ -65,11 +77,30 @@ public static String extractText(Cell cell, FormulaEvaluator evaluator, Sheet sh
6577
case BOOLEAN:
6678
return String.valueOf(value.getBooleanValue());
6779
case NUMERIC:
68-
double number = value.getNumberValue();
69-
if (number == Math.floor(number)) {
70-
return String.valueOf((int) number);
80+
if (DateUtil.isCellDateFormatted(cell)) {
81+
// Get the date value from the cell
82+
Date dateCellValue = cell.getDateCellValue();
83+
84+
// Convert java.util.Date to java.time.LocalDateTime
85+
LocalDateTime localDateTime = dateCellValue.toInstant()
86+
.atZone(ZoneId.systemDefault()).toLocalDateTime();
87+
88+
// Define a DateTimeFormatter with a specific pattern
89+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PARAMETER_DATE_FORMAT);
90+
91+
// Format LocalDateTime using DateTimeFormatter
92+
String formattedDate = localDateTime.format(formatter);
93+
94+
return formattedDate;
95+
}
96+
else {
97+
double number = value.getNumberValue();
98+
if (number == Math.floor(number)) {
99+
return String.valueOf((int) number);
100+
}
101+
102+
return String.valueOf(value.getNumberValue());
71103
}
72-
return String.valueOf(value.getNumberValue());
73104
case STRING:
74105
return value.getStringValue();
75106
default:

0 commit comments

Comments
 (0)