Here you can find the source of formatCpfCnpj(String cpfCnpj)
public static String formatCpfCnpj(String cpfCnpj)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import javax.swing.text.MaskFormatter; public class Main { public static String formatCpfCnpj(String cpfCnpj) { if (cpfCnpj.length() == 9) return format("###.###.###-##", cpfCnpj); else if (cpfCnpj.length() == 14) return format("##.###.###/####-##", cpfCnpj); else/*from w ww. j a v a 2s . c o m*/ return ""; } private static String format(String pattern, Object value) { MaskFormatter mask; try { mask = new MaskFormatter(pattern); mask.setValueContainsLiteralCharacters(false); return mask.valueToString(value); } catch (ParseException e) { throw new RuntimeException(e); } } }