2019年7月31日 星期三

[java]電話號碼/手機號碼隱碼

public static String phoneNumberToMaskFormat(final String phoneNumber)
{
if (StringUtils.isEmpty(phoneNumber))
{
return "";
}
final String cleanNumber = phoneNumber.replaceAll("[\\(\\)\\- ]*", "");//去除符號()-
if (StringUtils.isNotEmpty(cleanNumber) && cleanNumber.length() > 4)
{
//去符號讓第2-5個數字替換成星
//例:(02)2960-3456=>0****03456
//例:(037)364220=>0****4220
return cleanNumber.replaceAll("^(\\d{1})(\\d{4})(.*)$", "$1****$3");
}
//cleanNumber不超過4碼時
return "******";

}

public static String mobileNumberToMaskFormat(final String mobileNumber)
{
if (StringUtils.isEmpty(mobileNumber))
{
return "";
}
//留頭4字及尾3字,中間3字替換成星
//例:0912123456=>0912***456
if (mobileNumber.length() == 10)
{
return mobileNumber.replaceAll("^(.{4})(.*)(.{3})$", "$1***$3");
}
//mobileNumber不為10碼時
return "******";

}

[java]日期的年份隱碼

public static String yearToMaskFormat(final Date date)
{
if (date == null)
{
return "";
}
//將年換成星,例:1955/04/01=>****/04/01
final DateFormat df = new SimpleDateFormat("yyyyMMdd");
try
{
return df.format(date).replaceAll("^(\\d{4})(\\d{2})(\\d{2})$", "**** /$2/$3");
}
catch (final Exception e)
{
e.printStackTrace();
}
return "******";

}

[java]身分證號隱碼

public static String idToMaskEnd4Format(final String id)
{
if (StringUtils.isEmpty(id))
{
return "";
}
//尾4字替換成星
//例:A123456789=>A12345****
if (id.length() == 10)
{
return id.replaceAll("^(.*)(\\d{4})$", "$1****");
}
//id不為10碼時
return "******";
}
^