Skip to content

Commit be323d6

Browse files
Changed from DateTimeFormatter to LocalDate in date parse for validity of the date.
Signed-off-by: Johannes Tegnér <[email protected]> (cherry picked from commit 726560f)
1 parent 16e4ed3 commit be323d6

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/main/java/dev/personnummer/Personnummer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package dev.personnummer;
22

3+
import java.time.DateTimeException;
34
import java.time.LocalDate;
45
import java.time.LocalDateTime;
5-
import java.time.format.DateTimeFormatter;
6-
import java.time.format.DateTimeParseException;
76
import java.util.Objects;
87
import java.util.regex.*;
98

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

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

165165
this.isMale = Integer.parseInt(Character.toString(this.numbers.charAt(2))) % 2 == 1;
@@ -191,7 +191,7 @@ public Personnummer(String personnummer) throws PersonnummerException {
191191
public String toString() {
192192
return format();
193193
}
194-
194+
195195
/**
196196
* get a DateTime object from the Peronnummer object's date values of Date Month and Time.
197197
* @return DateTime object from the personnummer object

0 commit comments

Comments
 (0)