From 701fc5b193c3bbea8dc058e1ef5ab8ec7c9dc6c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Henninger?= Date: Wed, 25 Sep 2024 11:18:59 +0200 Subject: [PATCH] Switching from java.util.date to java.time API --- .../reportingdata/CSVReportingDataParser.java | 23 +++++++++---------- .../reportingdata/ContactAttempt.java | 7 ++++-- .../reportingdata/ReportingDataParser.java | 16 +++++++------ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/CSVReportingDataParser.java b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/CSVReportingDataParser.java index 353d3557..1ec5f6ca 100644 --- a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/CSVReportingDataParser.java +++ b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/CSVReportingDataParser.java @@ -5,10 +5,10 @@ import java.io.InputStreamReader; import java.nio.file.Path; import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; -import java.util.concurrent.TimeUnit; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import com.opencsv.CSVParser; import com.opencsv.CSVParserBuilder; @@ -68,20 +68,19 @@ public void parseReportingData(ReportingData reportingData, SurveyRawData data, } } - public long convertToTimestamp(String rowTimestamp) { - SimpleDateFormat dateFormat = new SimpleDateFormat(Constants.REPORTING_DATA_INPUT_DATE_FORMAT); - dateFormat.setTimeZone(TimeZone.getTimeZone("CET")); - Date parsedDate = null; + public long convertToTimestamp(String dateString) { + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(Constants.REPORTING_DATA_INPUT_DATE_FORMAT); + LocalDateTime parsedDate = null; try { - parsedDate = dateFormat.parse(rowTimestamp); - } catch (ParseException e1) { - log.error("Parsing error : {}", e1.getMessage()); + parsedDate = LocalDateTime.parse(dateString, dateFormat); + } catch (DateTimeParseException e) { + log.error("Parsing error : {}", e.getMessage()); } if (parsedDate == null) { log.error("Parsing error : the parsed date is null"); return 0L; } - return parsedDate.getTime(); + return parsedDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** diff --git a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ContactAttempt.java b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ContactAttempt.java index e2791196..5672202a 100644 --- a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ContactAttempt.java +++ b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ContactAttempt.java @@ -1,5 +1,8 @@ package fr.insee.kraftwerk.core.extradata.reportingdata; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.Date; import lombok.Getter; @@ -22,8 +25,8 @@ public ContactAttempt(String status, long timestamp) { this.timestamp = timestamp; } - public Date getDate() { - return new Date(timestamp); + public LocalDateTime getDate() { + return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()); } diff --git a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ReportingDataParser.java b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ReportingDataParser.java index 18376a4f..fcfdb3ac 100644 --- a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ReportingDataParser.java +++ b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/ReportingDataParser.java @@ -10,8 +10,10 @@ import fr.insee.kraftwerk.core.utils.files.FileUtilsInterface; import lombok.extern.log4j.Log4j2; -import java.util.Date; -import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; @@ -19,8 +21,8 @@ public abstract class ReportingDataParser { public static final String DATE_SUFFIX = "_DATE"; - private final SimpleDateFormat reportingDataOutputDateFormat = - new SimpleDateFormat(Constants.REPORTING_DATA_OUTPUT_DATE_FORMAT); + private final DateTimeFormatter reportingDataOutputDateFormat = + DateTimeFormatter.ofPattern(Constants.REPORTING_DATA_OUTPUT_DATE_FORMAT); Group reportingGroup; private int maxStates = 0; private int maxAttempts = 0; @@ -223,7 +225,7 @@ private void addReportingDataUEToQuestionnaire(SurveyRawData surveyRawData, Repo } if(reportingDataUE.getSurveyValidationDateTimeStamp() != null){ questionnaire.getAnswers().getSubGroup(Constants.REPORTING_DATA_GROUP_NAME).getInstance(Constants.REPORTING_DATA_PREFIX_NAME + reportingDataUE.getIdentifier()).putValue(Constants.REPORTING_DATA_SURVEY_VALIDATION_NAME, - reportingDataOutputDateFormat.format(new java.util.Date(reportingDataUE.getSurveyValidationDateTimeStamp()))); + reportingDataOutputDateFormat.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(reportingDataUE.getSurveyValidationDateTimeStamp()),ZoneId.systemDefault()))); } } @@ -253,7 +255,7 @@ private void addContactOutcome(ReportingDataUE reportingDataUE, QuestionnaireDat questionnaire.getAnswers().getSubGroup(Constants.REPORTING_DATA_GROUP_NAME) .getInstance(Constants.REPORTING_DATA_PREFIX_NAME + reportingDataUE.getIdentifier()) .putValue(Constants.OUTCOME_DATE, - reportingDataOutputDateFormat.format(new Date(contactOutcome.getDateEndContact()))); + reportingDataOutputDateFormat.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(contactOutcome.getDateEndContact()),ZoneId.systemDefault()))); } questionnaire.getAnswers().getSubGroup(Constants.REPORTING_DATA_GROUP_NAME) .getInstance(Constants.REPORTING_DATA_PREFIX_NAME + reportingDataUE.getIdentifier()) @@ -270,7 +272,7 @@ private void addStates(ReportingDataUE reportingDataUE, QuestionnaireData questi questionnaire.getAnswers().getSubGroup(Constants.REPORTING_DATA_GROUP_NAME) .getInstance(Constants.REPORTING_DATA_PREFIX_NAME + reportingDataUE.getIdentifier()) .putValue(Constants.STATE_SUFFIX_NAME + "_" + k + DATE_SUFFIX, - reportingDataOutputDateFormat.format(new Date((reportingDataUE.getStates().get(k - 1)).getTimestamp()))); + reportingDataOutputDateFormat.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(reportingDataUE.getStates().get(k - 1).getTimestamp()),ZoneId.systemDefault()))); } questionnaire.getAnswers().getSubGroup(Constants.REPORTING_DATA_GROUP_NAME) .getInstance(Constants.REPORTING_DATA_PREFIX_NAME + reportingDataUE.getIdentifier())