Class Iban

java.lang.Object
fr.marcwrobel.jbanking.iban.Iban
All Implemented Interfaces:
Serializable

public final class Iban extends Object implements Serializable
An International Bank Account Number (IBAN) Code as specified by the ISO 13616:2007 standard.

An IBAN consists of a two-letter ISO 3166-1 country code, followed by two check digits and up to thirty alphanumeric characters for a BBAN (Basic Bank Account Number) which has a fixed length per country and, included within it, a bank identifier with a fixed position and a fixed length per country. The check digits are calculated based on the scheme defined in ISO/IEC 7064 (MOD97-10). Note that an IBAN is case-insensitive.

Except the national check digit, which is pretty common, the following information were considered too specific to be part of the jbanking API :

  • account type (Bulgaria, Brasil, Guatemala),
  • account currency (Guatemala, Mauritius, Seychelles),
  • balance account number (Belarus),
  • identification number (Iceland),
  • owner account number (Brasil),
  • reserved characters (Costa Rica, Mauritius).

This class handles validation of the check digit and validation of the BBAN structure.

Instances of this class are immutable and thread-safe.

This class implements Serializable for convenience, but you are encouraged to use the normalized string representation if possible. Note that no validity check is done during deserialization.

Usage:

 // Validate an IBAN
 Assertions.assertTrue(Iban.isValid(" fr2531682128768051490609537 "));

 // Get IBAN information
 Iban iban = new Iban(" fr2531682128768051490609537 ");
 Assertions.assertEquals("FR2531682128768051490609537", iban.toString());
 Assertions.assertEquals("FR25 3168 2128 7680 5149 0609 537", iban.toPrintableString());
 Assertions.assertEquals("FR", iban.getCountryCode());
 Assertions.assertEquals("25", iban.getCheckDigit());
 Assertions.assertEquals("31682128768051490609537", iban.getBban());
 Assertions.assertEquals("31682", iban.getBankIdentifier());
 Assertions.assertEquals("12876", iban.getBranchIdentifier().get());
 Assertions.assertEquals("80514906095", iban.getAccountNumber());
 Assertions.assertEquals("37", iban.getNationalCheckDigit().get());
 
Since:
1.0
See Also:
  • Field Details

  • Constructor Details

    • Iban

      public Iban(IsoCountry country, String bban)
      Create a new IBAN from the given country code and BBAN.

      This method is neither sensitive to the case nor to the presence of leading or trailing spaces.

      Parameters:
      country - a non-null IsoCountry
      bban - a non-null string
      Throws:
      IllegalArgumentException - if either the IsoCountry or BBAN is null
      IbanFormatException - if a valid IBAN could not be calculated using the given IsoCountry and BBAN
    • Iban

      public Iban(String s)
      Create a new IBAN from the given string.

      This method is neither sensitive to the case nor to the presence of leading or trailing spaces.

      Parameters:
      s - a non-null string
      Throws:
      IllegalArgumentException - if the given string is null
      IbanFormatException - if the given string is not a valid IBAN
  • Method Details

    • isValid

      public static boolean isValid(String s)
      Validates the given IBAN String.

      This method is neither sensitive to the case nor to the presence of leading or trailing spaces.

      Parameters:
      s - a string, may be null
      Returns:
      true if the given String is a valid IBAN, false otherwise
    • getCountryCode

      public String getCountryCode()
      Extract the ISO 3166-1-alpha-2 country code from this IBAN.
      Returns:
      a non-null string representing this IBAN ISO 3166-1-alpha-2 country code
    • getCountry

      public IsoCountry getCountry()
      Gets this IBAN IsoCountry.
      Returns:
      a non-null IsoCountry
    • getCheckDigit

      public String getCheckDigit()
      Extract the check digit from this IBAN.
      Returns:
      a non-null string
    • getBban

      public String getBban()
      Extract the BBAN from this IBAN.
      Returns:
      a non-null string
    • getBankIdentifier

      public String getBankIdentifier()
      Extract the bank identifier (also known as bank code) from this IBAN.
      Returns:
      a non-null string
      Since:
      4.0.0
    • getBranchIdentifier

      public Optional<String> getBranchIdentifier()
      Extract the branch identifier (also known as branch code) from this IBAN, if possible.
      Returns:
      a non-null optional string
      Since:
      4.0.0
    • getNationalCheckDigit

      public Optional<String> getNationalCheckDigit()
      Extract the national check digit (also known as check character) from this IBAN, if possible.

      The type of the check digit and the algorithm used to compute it varies by country (RIB key in France, CIN in Italy...).

      Returns:
      a non-null optional string
      Since:
      4.0.0
    • getAccountNumber

      public String getAccountNumber()
      Extract the national account number from this IBAN.
      Returns:
      a non-null string representing this IBAN account number
      Since:
      4.0.0
    • toPrintableString

      public String toPrintableString()
      Gets the printable version of this IBAN.

      When printed on paper, the IBAN is expressed in groups of four characters separated by a single space, the last group being of variable length

      Returns:
      a non-null string representing this IBAN formatted for printing
    • toString

      public String toString()
      Returns a normalized string representation of this IBAN.

      Normalized means the string is:

      • made of uppercase characters
      • contains no spaces
      Overrides:
      toString in class Object
      Returns:
      a normalized string representation of this IBAN
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object