Long or String for Norwegian birth number

I have a class called Person that looks something like this:

public abstract class Person {
    private String birthNo;
    ...
}

Numbers of Norwegian births are 11 digits and are constructed according to the algorithm described on this page on Wikipedia . I cannot use int since most birth numbers are greater than Integer.MAX_VALUE.

I heard that when deciding whether to use a String or integer to store numeric values, you should ask yourself: do I need to perform mathematical operations on this value?

I do this on birthNo, but many operations become somewhat easier with String charAt(int i). If I were to keep birth numbers for how long, I would have to use modulo and division to get the desired digit:

int ninth = Character.getNumericValue(birthNo.charAt(8));

against.

int ninth = birthNo / 100 % 10

, , - (, 9- ) , ISBN.

String , , , "11 ", .

? ?

+4
3

. . (DDMMYY), .

BirthNumber LocalDate int String , , . , .

, , :

import java.time.LocalDate;

public class DNumberLocalDate {

    private final LocalDate localDate;
    private final boolean isDNumber;

    public DNumberLocalDate(LocalDate localDate, boolean isDNumber) {
        this.localDate = localDate;
        this.isDNumber = isDNumber;
    }

    public DNumberLocalDate(LocalDate localDate) {
        this(localDate, false);
    }

    public int getDayOfMonth() {
        return isDNumber ? localDate.getDayOfMonth() + 40 : localDate.getDayOfMonth();
    }

}
+3

- , : . , - , . String. - , , .

BigDecimal BigInteger.

+2

, ( 0 ) u String private. U 2 geter 1 . vars , , oop. Greetz

-1

Source: https://habr.com/ru/post/1584749/


All Articles