Skip to content

Commit

Permalink
Switching from java.util.date to java.time API
Browse files Browse the repository at this point in the history
  • Loading branch information
loichenninger committed Sep 25, 2024
1 parent e981f9f commit 701fc5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
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;

@Log4j2
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;
Expand Down Expand Up @@ -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())));
}
}

Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down

0 comments on commit 701fc5b

Please sign in to comment.