43 lines
1.1 KiB
Java
43 lines
1.1 KiB
Java
package com.rjconsultores.ventaboletos.utilerias;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import javax.swing.text.MaskFormatter;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
public class DocumentosUtil {
|
|
|
|
private static final String CPF_MASK = "###.###.###-##";
|
|
private static final String CNPJ_MASK = "##.###.###/####-##";
|
|
|
|
public static String formataCpf(String cpf) {
|
|
try {
|
|
if(StringUtils.isNotBlank(cpf)) {
|
|
cpf = StringHelper.preencherZeroEsquerda(cpf.replace("[^0-9]", ""), 11);
|
|
MaskFormatter mask = new MaskFormatter(CPF_MASK);
|
|
mask.setValueContainsLiteralCharacters(false);
|
|
return mask.valueToString(cpf);
|
|
}
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String formataCnpj(String cnpj) {
|
|
try {
|
|
if(StringUtils.isNotBlank(cnpj)) {
|
|
cnpj = StringHelper.preencherZeroEsquerda(cnpj.replace("[^0-9]", ""), 14);
|
|
MaskFormatter mask = new MaskFormatter(CNPJ_MASK);
|
|
mask.setValueContainsLiteralCharacters(false);
|
|
return mask.valueToString(cnpj);
|
|
}
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|