Skip to content

Commit

Permalink
Changed from DateTimeFormatter to LocalDate in date parse for validit…
Browse files Browse the repository at this point in the history
…y of the date.

Signed-off-by: Johannes Tegnér <[email protected]>
(cherry picked from commit 726560f)
  • Loading branch information
Johannestegner committed Apr 25, 2023
1 parent 16e4ed3 commit be323d6
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/dev/personnummer/Personnummer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package dev.personnummer;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Objects;
import java.util.regex.*;

Expand Down Expand Up @@ -157,9 +156,10 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc
this.controlNumber = matches.group(7);

try {
DateTimeFormatter.ofPattern("yyyy MM dd").parse(String.format("%s %s %02d", this.fullYear, this.month, this.realDay));
} catch (DateTimeParseException e) {
throw new PersonnummerException("Invalid personal identity number.");
//noinspection ResultOfMethodCallIgnored
LocalDate.of(Integer.parseInt(this.fullYear), Integer.parseInt(this.month), this.realDay);
} catch (DateTimeException e) {
throw new PersonnummerException("Invalid personal identity number: " + e.getMessage());
}

this.isMale = Integer.parseInt(Character.toString(this.numbers.charAt(2))) % 2 == 1;
Expand Down Expand Up @@ -191,7 +191,7 @@ public Personnummer(String personnummer) throws PersonnummerException {
public String toString() {
return format();
}

/**
* get a DateTime object from the Peronnummer object's date values of Date Month and Time.
* @return DateTime object from the personnummer object
Expand Down

0 comments on commit be323d6

Please sign in to comment.