Class PhoneNumberUtil

java.lang.Object
cloud.opencode.base.sms.util.PhoneNumberUtil

public final class PhoneNumberUtil extends Object
Phone Number Utility 手机号码工具类

Utilities for phone number validation and formatting.

手机号码验证和格式化工具。

Features | 主要功能:

  • Phone number validation (China, international) - 手机号验证(中国、国际)
  • Phone number normalization - 手机号标准化
  • Country code mapping - 国家代码映射
  • Phone number masking - 手机号脱敏

Usage Examples | 使用示例:

boolean valid = PhoneNumberUtil.isValid("+8613800138000");
String normalized = PhoneNumberUtil.normalize("138-0013-8000");
String masked = PhoneNumberUtil.mask("13800138000"); // "138****8000"

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(1) - phone numbers have bounded length (max ~15 digits) - 时间复杂度: O(1),手机号长度有限(最多约 15 位)
  • Space complexity: O(1) - pre-compiled regex patterns - 空间复杂度: O(1) 预编译正则表达式
Since:
JDK 25, opencode-base-sms V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isValid

      public static boolean isValid(String phoneNumber)
      Validate phone number 验证手机号码
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      true if valid | 如果有效返回true
    • isChinaMobile

      public static boolean isChinaMobile(String phoneNumber)
      Validate China mobile number 验证中国手机号码
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      true if valid | 如果有效返回true
    • normalize

      public static String normalize(String phoneNumber)
      Normalize phone number 标准化手机号码
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      the normalized number | 标准化的号码
    • formatWithCountryCode

      public static String formatWithCountryCode(String phoneNumber, String countryCode)
      Format with country code 使用国家代码格式化
      Parameters:
      phoneNumber - the phone number | 手机号码
      countryCode - the country code (e.g., "CN", "US") | 国家代码
      Returns:
      the formatted number | 格式化的号码
    • formatChina

      public static String formatChina(String phoneNumber)
      Format China mobile number 格式化中国手机号码
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      the formatted number | 格式化的号码
    • mask

      public static String mask(String phoneNumber)
      Mask phone number for display 掩码手机号码用于显示
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      the masked number | 掩码后的号码
    • getCountryDialingCode

      public static String getCountryDialingCode(String countryCode)
      Get country code 获取国家代码
      Parameters:
      countryCode - the country code (e.g., "CN", "US") | 国家代码
      Returns:
      the dialing prefix | 拨号前缀
    • extractCountryCode

      public static String extractCountryCode(String phoneNumber)
      Extract country code from phone number 从手机号码提取国家代码
      Parameters:
      phoneNumber - the phone number | 手机号码
      Returns:
      the country code or null | 国家代码或null