The Best Way to Handle Phone Numbers – Keep IT Simple with Simon Martinelli

Processing phone numbers seems complicated at first glance because of the many different formats. In this post, I’ll show you that phonenumberutil with libphonenumber, it becomes child’s play. I’ll also recommend how to store the phone number in the database.

Introduction Googles phonenumberutil

According to the GitHub repository  libphone number is:

Google’s common Java, C++ and JavaScript library for parsing, formatting, and validating international phone chile phone number library numbers. The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).
Let’s see the library in action. The source code is available here: https://github.com/simasch/libphonenumber-demo

Creating and Formatting Phone Numbers
The main class of libphonenumber is, no surprise, the class Phonenumber. You’ll want to use a phone number in two ways to create a phone number.

First from country code and

chile phone number library

Phonenumber.PhoneNumber swissPhoneNumber = new Phonenumber.PhoneNumber();
swissPhoneNumber.setCountryCode(41)
swissPhoneNumber.setNationalNumber
We can now format the phone number in different ways:

NumberUtil phoneNrUtil = PhoneNumberl.getInstance();
System.out.println(phoneUtil.format(swissPhoneNumber, .PhoneNumberFormat.INTERNATIONAL));
System.out.println(il.format(swissPhoneNumber, Util.PhoneNumberFormat.NATIONAL));

This will prin Parsing Phone Numbers

But what if we get the phone number as a string? No worries, the PhoneUtil cannot only format but also parse phone numbers:

Phonenumber.PhoneNumber parsedSwissPhoneNumber =NumberUtil.parse(“032 455 66 77”, “CH”);
The linkedin live, the most human way to network on the social network first argument is the  phone number as a string, and the second parameter is the region we expect the number to be from; this is only used if the number string is not an international number.

Validating Phone il Numbers
Now that we can parse strings to phone numbers, how can we check if the phone number is valid?
Also, here, l, has you covered it as it has metadata of the phone numbers of many regions?

Phonenumber.PhoneNumber

invalidSwissPhoneNumber = .parse(“032 631 11 2”, “CH”);
System.out.println(isValidNumber(invalidSwissPhoneNumber));
The code above will print false as the phone number is too short.

Conclusion and Recommendation
Google’s libphonnumber is very powerful, and it has even more features, like finding phone numbers in text, getting the type of the job data number, and so on. In my opinion, it’s the go-to library when it comes to processing phone numbers.

Finally, to answer how a phone number should be stored in the database, I recommend storing the country code and national number separately as numbers. That way, you can directly create a libphonenumber Phonenumber  from the data and format it when needed.

Simon Martinelli phonenumberutil

Follow Me
Simon Martinelli
I’m a Java Champion, a Vaadin Champion, and an Oracle ACE Pro. I write and speak about architecture and software development with Java.
I’m fighting for simplicity in software  development.
Previous
Fly.io and Neon: The Perfect Combination to Run Your Java Applications

Scroll to Top