Java Swing Text Format formatCpfCnpj(String cpfCnpj)

Here you can find the source of formatCpfCnpj(String cpfCnpj)

Description

format Cpf Cnpj

License

Open Source License

Declaration

public static String formatCpfCnpj(String cpfCnpj) 

Method Source Code

//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);
        }
    }
}

Related

  1. format(String pattern, Object value)
  2. formata(String s, String mascara)
  3. formatAreaImperial(BigDecimal areaDec)
  4. formatAreaMetric(BigDecimal area)
  5. formatCEP(String str)
  6. formatCpfCNPJ(String str)
  7. formatNumericString(String string, String mask)
  8. getBidiVisualPosition(final int start, final int end, final Element neighbouringElement, final int pos, final Position.Bias b0, final Position.Bias[] biasRet, final int length, final boolean toWest, final boolean isLTR)
  9. getElementByPosition(final Element rootElement, final int pos)