Skip to content

Commit

Permalink
Fix date parse for yyyy-MM-ddTHH:mm:ss pattern (#215)
Browse files Browse the repository at this point in the history
* Fix date parse for yyyy-MM-ddTHH:mm:ss pattern

* Fix Date format when T present

* Fix Date format when T present
  • Loading branch information
arybakov-cgi authored Jul 25, 2023
1 parent 0703c18 commit e302cf6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

Expand All @@ -19,7 +19,7 @@ public class Student implements Serializable {
private String gender;
private String citizenship;
private LocalDate birthdate;
private LocalDateTime lastUpdateDate;
private Date lastUpdateDate;
private Address address;
private String grade;
private String gradProgram;
Expand Down Expand Up @@ -101,11 +101,12 @@ public void setBirthdate(LocalDate value) {
this.birthdate = value;
}

public LocalDateTime getLastUpdateDate() {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
public Date getLastUpdateDate() {
return lastUpdateDate;
}

public void setLastUpdateDate(LocalDateTime lastUpdateDate) {
public void setLastUpdateDate(Date lastUpdateDate) {
this.lastUpdateDate = lastUpdateDate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public class StudentCredentialDistribution {
private String program;
private String studentGrade;
private List<GradRequirement> nonGradReasons;

public LocalDateTime getLastUpdateDate() {
return lastUpdateDate == null ? LocalDateTime.now() : lastUpdateDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -218,7 +220,7 @@ private Student prepareStudentObj(StudentCredentialDistribution scd, List<Studen
std.setPen(pen);
std.setGrade(scd.getStudentGrade());
std.setGradProgram(scd.getProgram());
std.setLastUpdateDate(scd.getLastUpdateDate());
std.setLastUpdateDate(Date.from(scd.getLastUpdateDate().atZone(ZoneId.systemDefault()).toInstant()));
std.setGraduationData(new GraduationData());
std.setNonGradReasons(getNonGradReasons(scd.getNonGradReasons()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext d
formatter = DateTimeFormatter.ofPattern(SECOND_DEFAULT_DATE_FORMAT);
LocalDate localDate = LocalDate.parse(dateAsString, formatter);
return localDate.atStartOfDay();
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 10 && dateAsString.contains("/") && dateAsString.contains(" ")) {
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 10 && dateAsString.length() <= 19 && dateAsString.contains("/") && dateAsString.contains(" ")) {
formatter = DateTimeFormatter.ofPattern(SECOND_DEFAULT_DATE_TIME_FORMAT);
return LocalDateTime.parse(dateAsString, formatter);
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 10 && dateAsString.contains("-") && dateAsString.contains(" ")) {
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 10 && dateAsString.length() <= 19 && dateAsString.contains("-") && dateAsString.contains(" ")) {
formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
return LocalDateTime.parse(dateAsString, formatter);
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 19 && dateAsString.contains("/") && dateAsString.contains("T")) {
formatter = DateTimeFormatter.ofPattern(SECOND_DEFAULT_DATE_TIME_FORMAT);
return LocalDateTime.parse(StringUtils.replace(StringUtils.substringBefore(dateAsString, "."), "T", " "), formatter);
} else if(StringUtils.isNotBlank(dateAsString) && dateAsString.length() > 19 && dateAsString.contains("-") && dateAsString.contains("T")) {
formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
return LocalDateTime.parse(StringUtils.replace(StringUtils.substringBefore(dateAsString, "."), "T", " "), formatter);
} else if(StringUtils.isNotBlank(dateAsString)) {
return LocalDateTime.parse(dateAsString, formatter);
}
Expand Down

0 comments on commit e302cf6

Please sign in to comment.