Skip to content

personnummer/java

This branch is up to date with master.

Folders and files

NameName
Last commit message
Last commit date
Oct 16, 2024
Jan 24, 2025
Feb 27, 2024
Oct 8, 2020
Mar 13, 2023
Jun 17, 2020
Mar 13, 2023
Jan 8, 2025
Dec 20, 2024
Jul 11, 2024
Oct 20, 2017

Repository files navigation

Personnummer

GitHub Workflow Status

Validate Swedish personal identity numbers.

Installation

Add the package to your maven or gradle configuration.
If you prefer to use the package from github rather than maven-central, add the repository as well.

<dependency>
  <groupId>dev.personnummer</groupId>
  <artifactId>personnummer</artifactId>
  <version>3.*.*</version>
</dependency> 
plugins {
    id 'maven'
}

repositories {
    // If using maven central
    mavenCentral()
    // If you wish to use github
    maven {
      url "https://github.com/personnummer/java:personnummer"
    }
}

dependencies {
    configuration("dev.personnummer:personnummer")
}

For more information on how to install and authenticate with github packages, check this link.

Examples

Validation

import dev.personnummer.*;

class Test 
{
  public void TestValidation() 
  {
    Personnummer.valid("191212121212");    // => True
    Personnummer.valid("121212+1212");     // => True
    Personnummer.valid("20121212-1212");   // => True
  }
}

Format

// Short format (YYMMDD-XXXX)
(new Personnummer("1212121212")).format();
// => 121212-1212

// Short format for 100+ years old
(new Personnummer("191212121212")).format();
//=> 121212+1212

// Long format (YYYYMMDDXXXX)
Personnummer.parse("1212121212").format(true);
//=> 201212121212

Age

(new Personnummer("1212121212")).getAge();
//=> 7

Get sex

(new Personnummer("1212121212")).isMale();
//=> true
Personnummer.parse("1212121212").isFemale();
//=> false

Get date

(new Personnummer("1212121212")).getDate();
//=> 2012-12-12T00:00
(new Personnummer("9001010017")).getDate();
//=> 1990-01-01T00:00

See src/test//PersonnummerTest.java for more examples.

License

MIT