問題描述
使用 Java 將電話號碼轉(zhuǎn)換為國際格式 (E.164) 的最佳方法是什么?
What is the best way for converting phone numbers into international format (E.164) using Java?
給定一個電話號碼"和國家/地區(qū) ID(比如說 ISO 國家/地區(qū)代碼),我想將其轉(zhuǎn)換為標準 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?
附:電話號碼"可以是公眾可以識別的任何內(nèi)容 - 例如
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
最后一個是我最喜歡的 - 這是一些人在英國寫他們的號碼的方式,這意味著你應(yīng)該使用 +44 或你應(yīng)該使用 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 格式編號應(yīng)為全數(shù)字,并使用完整的國際國家代碼(例如+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));
這篇關(guān)于使用 Java 將電話號碼轉(zhuǎn)換為國際格式 (E.164) 的最佳方法是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!