使用 Java 将电话号码转换为国际格式 (E.164) 的最佳方法是什么? [英] What is the best way for converting phone numbers into international format (E.164) using Java?

查看:31
本文介绍了使用 Java 将电话号码转换为国际格式 (E.164) 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Java 将电话号码转换为国际格式 (E.164) 的最佳方法是什么?

What is the best way for converting phone numbers into international format (E.164) using Java?

给定一个电话号码"和国家/地区 ID(比如说 ISO 国家/地区代码),我想将其转换为标准 E.164 国际格式电话号码.

Given a 'phone number' and a country id (let's say an ISO country code), I would like to convert it into a standard E.164 international format phone number.

我确信我可以很容易地手动完成 - 但我不确定它在所有情况下都能正常工作.

I am sure I can do it by hand quite easily - but I would not be sure it would work correctly in all situations.

您会推荐哪个 Java 框架/库/实用程序来完成此操作?

Which Java framework/library/utility would you recommend to accomplish this?

附:电话号码"可以是公众可以识别的任何内容 - 例如

P.S. The 'phone number' could be anything identifiable by the general public - such as

* (510) 786-0404
* 1-800-GOT-MILK
* +44-(0)800-7310658

最后一个是我最喜欢的 - 这是一些人在英国写他们的号码的方式,这意味着你应该使用 +44 或你应该使用 0.

that last one is my favourite - it is how some people write their number in the UK and means that you should either use the +44 or you should use the 0.

E.164 格式编号应为全数字,并使用完整的国际国家代码(例如+44)

The E.164 format number should be all numeric, and use the full international country code (e.g.+44)

推荐答案

Google 提供了一个用于处理电话号码的库.与他们用于 Android 的相同

Google provides a library for working with phone numbers. The same one they use for Android

http://code.google.com/p/libphonenumber/

String swissNumberStr = "044 668 18 00"
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
  PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
  System.err.println("NumberParseException was thrown: " + e.toString());
}

// Produces "+41 44 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.INTERNATIONAL));
// Produces "044 668 18 00"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.NATIONAL));
// Produces "+41446681800"
System.out.println(phoneUtil.format(swissNumberProto, PhoneNumberFormat.E164));

这篇关于使用 Java 将电话号码转换为国际格式 (E.164) 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆