How to format date in full?

5

Is there any class in java that works with long date formatting?

For example:

Entry: [dd / mm / yyyy] 2/27/2016

Check out: Twenty-seven of February 2016

I tried:

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Exemplo {

  public static void main(String[] args) {
    Scanner ler = new Scanner(System.in);

    String s;

    System.out.printf("Informe o nome da Cidade:\n");
    s = ler.nextLine();

    System.out.printf("\nResultado:\n");
    System.out.printf("%s\n", DataPorExtenso(s, new java.util.Date()));
  }

  public static String NomeDoMes(int i, int tipo) {
    String mes[] = {"janeiro", "fevereiro", "março", "abril",
      "maio", "junho", "julho", "agosto", "setembro", "outubro",
      "novembro", "dezembro"};
    if (tipo == 0)
       return(mes[i-1]); 
    else return(mes[i-1].substring(0, 3)); 
  }


  public static String DiaDaSemana(int i, int tipo) {
    String diasem[] = {"domingo", "segunda-feira", "terça-feira",
      "quarta-feira", "quinta-feira", "sexta-feira", "sábado"};
    if (tipo == 0)
       return(diasem[i-1]); 
    else return(diasem[i-1].substring(0, 3));
  }

  public static String DataPorExtenso(String cidade, java.util.Date dt) {
    int d = dt.getDate();
    int m = dt.getMonth()+1;
    int a = dt.getYear()+1900;

    Calendar data = new GregorianCalendar(a, m-1, d);
    int ds = data.get(Calendar.DAY_OF_WEEK);

    return(cidade + ", " + d + " de " + NomeDoMes(m, 0) + " de " +
      a + " (" + DiaDaSemana(ds, 1) + ").");
  }

}

The output was:

February 27, 2016

    
asked by anonymous 27.02.2016 / 12:09

1 answer

8

To leave in the format you want and with the month in full:

Date data =  new Date();
Locale local = new Locale("pt","BR");
DateFormat formato = new SimpleDateFormat("dd 'de' MMMM 'de' yyyy",local);
System.out.println(formato.format(data));

This will start, for example, 27 de Fevereiro de 2016 , that is, the day is not yet in full. I have not found anything native to Java that gives you back the numbers in the form you want. But implementing a method that does this is not too difficult either. Here is my version:

public static String getDiaPorExtenso(int dia) throws Exception {
    String dias[] = {"zero", "um", "dois", "três","quatro", "cinco", "seis", "sete", "oito", "nove"};
    String retorno = "";

    if (dia < 1 || dia > 31) {
        throw new Exception("Não existe esse dia em nenhum mês do ano");
    }
    else if (dia < 10) {
        retorno = dias[dia];
    }
    else if (dia < 20) {
        retorno = new String[]{
            "dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"
        }[dia - 10];
    }
    else if (dia < 30) {
        if (dia == 20) {
            retorno = "vinte";
        }
        else {
            retorno = "vinte e " + dias[dia - 20];
        }
    }
    else {
        if (dia == 30) {
            retorno = "trinta";
        }
        else {
            retorno = "trinta e " + dias[dia - 30];
        }
    }

    // Capitaliza apenas a primeira letra do dia.
    return retorno.substring(0, 1).toUpperCase() + retorno.substring(1);
}

Putting the two together, you can display the end result as follows:

Date data = new GregorianCalendar(2014, Calendar.FEBRUARY, 11).getTime();
Locale local = new Locale("pt","BR");
DateFormat formato = new SimpleDateFormat(" 'de' MMMM 'de' yyyy", local);
String dataFormatada = formato.format(data);

Calendar calendario = Calendar.getInstance();
calendario.setTime(data);
int dia = calendario.get(Calendar.DAY_OF_MONTH);

System.out.println(getDiaPorExtenso(dia) + dataFormatada);

Output: Onze de Fevereiro de 2014 .

Note:

Calendar calendario = Calendar.getInstance();
calendario.setTime(data);
int dia = calendario.get(Calendar.DAY_OF_MONTH);

is used instead of the more obvious data.getDate() because the latter method is deprecated .

See the full code and running on Ideone .

    
27.02.2016 / 15:19