Skip to content

Commit

Permalink
feat: add formatter for the date
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emanuelaepure10 committed Dec 27, 2023
1 parent ec50283 commit 96c7c95
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

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

import java.text.SimpleDateFormat
import java.time.LocalDate

import eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder
import eu.esdihumboldt.hale.common.instance.model.InstanceCollection
import eu.esdihumboldt.hale.common.schema.groovy.SchemaBuilder
Expand All @@ -26,6 +29,11 @@ class XLSInstanceWriterTestExamples {

Schema schema = createSchema()

// Declare a date in the "dd/mm/yyyy" format
def dateString1 = "25/12/2023"
def dateFormat1 = new SimpleDateFormat("dd/MM/yyyy")
def date1 = dateFormat1.parse(dateString1)

// create the instance collection
// concrete types are only strings, since the test is not able to choose the correct type in wizard
InstanceCollection instances = new InstanceBuilder(types: schema).createCollection {
Expand Down Expand Up @@ -59,6 +67,7 @@ class XLSInstanceWriterTestExamples {
name('other')
number('1')
description('other type')
date(date1)
}
}
}
Expand Down Expand Up @@ -91,9 +100,9 @@ class XLSInstanceWriterTestExamples {
name(String)
number(String)
description(String)
date(LocalDate)
}
}
return schema;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@

package eu.esdihumboldt.hale.io.xls;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Expand All @@ -30,6 +36,12 @@
*/
public class XLSUtil {

/**
* Parameter for the reader specifying how values imported from date cells
* should be formatted.
*/
public static final String PARAMETER_DATE_FORMAT = "yyyy-MM-dd";

/**
* Extract the text from a given cell. Formulas are evaluated, for blank or
* error cells <code>null</code> is returned
Expand Down Expand Up @@ -65,11 +77,30 @@ public static String extractText(Cell cell, FormulaEvaluator evaluator, Sheet sh
case BOOLEAN:
return String.valueOf(value.getBooleanValue());
case NUMERIC:
double number = value.getNumberValue();
if (number == Math.floor(number)) {
return String.valueOf((int) number);
if (DateUtil.isCellDateFormatted(cell)) {
// Get the date value from the cell
Date dateCellValue = cell.getDateCellValue();

// Convert java.util.Date to java.time.LocalDateTime
LocalDateTime localDateTime = dateCellValue.toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime();

// Define a DateTimeFormatter with a specific pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PARAMETER_DATE_FORMAT);

// Format LocalDateTime using DateTimeFormatter
String formattedDate = localDateTime.format(formatter);

return formattedDate;
}
else {
double number = value.getNumberValue();
if (number == Math.floor(number)) {
return String.valueOf((int) number);
}

return String.valueOf(value.getNumberValue());
}
return String.valueOf(value.getNumberValue());
case STRING:
return value.getStringValue();
default:
Expand Down

0 comments on commit 96c7c95

Please sign in to comment.