From be323d69dfc142e117f9f27a68598127f8676505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Tue, 25 Apr 2023 13:04:36 +0200 Subject: [PATCH] Changed from DateTimeFormatter to LocalDate in date parse for validity of the date. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Tegnér (cherry picked from commit 726560f9c300a461a225ef387081bcfd67fb46cf) --- src/main/java/dev/personnummer/Personnummer.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/dev/personnummer/Personnummer.java b/src/main/java/dev/personnummer/Personnummer.java index 1bc856f..43abe33 100644 --- a/src/main/java/dev/personnummer/Personnummer.java +++ b/src/main/java/dev/personnummer/Personnummer.java @@ -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.*; @@ -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; @@ -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