Java Swing Text Format formatCEP(String str)

Here you can find the source of formatCEP(String str)

Description

format CEP

License

Open Source License

Declaration

public static String formatCEP(String str) 

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 formatCEP(String str) {

        str = str.replaceAll("[\\D]", "");
        try {//w w w  .  j  a  va  2  s  .c o  m
            str = String.format("%08d", Long.parseLong(str));
            String pattern = "#####-###";
            try {
                MaskFormatter mask = new MaskFormatter(pattern);
                mask.setValueContainsLiteralCharacters(false);
                return mask.valueToString(str);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } catch (NumberFormatException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. formartCpf(String cpf)
  2. format(String pattern, Object value)
  3. formata(String s, String mascara)
  4. formatAreaImperial(BigDecimal areaDec)
  5. formatAreaMetric(BigDecimal area)
  6. formatCpfCnpj(String cpfCnpj)
  7. formatCpfCNPJ(String str)
  8. formatNumericString(String string, String mask)
  9. 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)