Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use java time classes inside Personnummer #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/main/java/dev/personnummer/Personnummer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.personnummer;

import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Objects;
Expand Down Expand Up @@ -45,10 +47,10 @@ public static Personnummer parse(String personnummer) throws PersonnummerExcepti


private final int realDay;
private final String fullYear;
private final Year fullYear;
private final String century;
private final String year;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The property name year may cause confusion. Perhaps decade is a better name?

String getDecade()
Year getYear()

private final String month;
private final Month month;
private final String day;
private final String numbers;
private final String controlNumber;
Expand All @@ -66,7 +68,7 @@ public String separator() {
return this.getAge() >= 100 ? "+" : "-";
}

public String getFullYear() {
public Year getFullYear() {
return fullYear;
}

Expand All @@ -79,7 +81,8 @@ public String getYear() {
}

public String getMonth() {
return month;
final int monthNumber = month.getValue();
return monthNumber < 10 ? "0" + monthNumber : Integer.toString(monthNumber);
}

public String getDay() {
Expand All @@ -95,7 +98,7 @@ public String getControlNumber() {
}

public int getAge() {
return (LocalDate.of(Integer.parseInt(fullYear), Integer.parseInt(month), realDay).until(LocalDate.now())).getYears();
return (LocalDate.of(fullYear.getValue(), month, realDay).until(LocalDate.now())).getYears();
}

/**
Expand Down Expand Up @@ -139,23 +142,24 @@ public Personnummer(String personnummer, Options options) throws PersonnummerExc
this.realDay = day;
this.century = century;
this.year = decade;
this.fullYear = century + decade;
this.month = matches.group(3);
this.fullYear = Year.of(Integer.parseInt(century + decade));
String tempMonth = matches.group(3);
this.day = matches.group(4);
this.numbers = matches.group(6) + matches.group(7);
this.controlNumber = matches.group(7);

try {
DateTimeFormatter.ofPattern("yyyy MM dd").parse(String.format("%s %s %02d", this.fullYear, this.month, this.realDay));
DateTimeFormatter.ofPattern("yyyy MM dd").parse(String.format("%s %s %02d", this.fullYear, tempMonth, this.realDay));
} catch (DateTimeParseException e) {
throw new PersonnummerException("Invalid personal identity number.");
}
this.month = Month.of(Integer.parseInt(tempMonth));

this.isMale = Integer.parseInt(Character.toString(this.numbers.charAt(2))) % 2 == 1;

// The format passed to Luhn method is supposed to be YYmmDDNNN
// Hence all numbers that are less than 10 (or in last case 100) will have leading 0's added.
if (luhn(String.format("%s%s%s%s", this.year, this.month, this.day, matches.group(6))) != Integer.parseInt(this.controlNumber)) {
if (luhn(String.format("%s%s%s%s", this.year, this.getMonth(), this.day, matches.group(6))) != Integer.parseInt(this.controlNumber)) {
throw new PersonnummerException("Invalid personal identity number.");
}
}
Expand Down Expand Up @@ -194,7 +198,7 @@ public String format() {
* @return Formatted personal identity number.
*/
public String format(boolean longFormat) {
return (longFormat ? fullYear : year) + month + day + (longFormat ? "" : separator()) + numbers;
return (longFormat ? fullYear : year) + getMonth() + day + (longFormat ? "" : separator()) + numbers;
}

/**
Expand Down