Java tutorial
package nc.noumea.mairie.appock.core.utility; /*- * #%L * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma * %% * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import org.apache.commons.lang.StringUtils; import com.google.i18n.phonenumbers.PhoneNumberUtil; import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat; import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; /** * Classe utilitaire pour manipuler les numros de tlphone, en particulier en NC. * * @author AgileSoft.NC */ public class TelUtil { /** format utilis par la librairie phoneNumber pour la Nouvelle-Caldonie */ public static final String FORMAT_PHONE_NC = "NC"; /** * Formatte un numro de tlphone, en se basant sur la librairie google libPhoneNumber. Si le numro n'est pas reconnu, le numro est retourn tel quel * sans les blancs devant/derrire. * * @param tel numro de tlphone traiter * @return "" si le tlphone en entre est "vide" (null ou blanc) */ public static String formatteTel(String tel) { if (StringUtils.isBlank(tel)) { return ""; } try { PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); PhoneNumber phoneNumber = phoneUtil.parse(tel, FORMAT_PHONE_NC); return phoneUtil.format(phoneNumber, PhoneNumberFormat.NATIONAL); } catch (Exception e) { // en cas d'erreur, on se contente de retourner le tel pass en entre, sans les blancs devant/derrire return StringUtils.trimToEmpty(tel); } } }