Domanda

GregorianCalendar startDate = new GregorianCalendar(2009, Calendar.JANUARY, 1);
SimpleDateFormat sdf = new SimpleDateFormat("d/m/yyyy"); 
public void setStart()
{
    startDate.setLenient(false);
    System.out.println(sdf.format(startDate.getTime()));
}

Quando eseguo questo codice, ottengo l'1 / 1/2009. Ottengo anche la stessa cosa quando cambio i mesi. Cosa c'è che non va e come posso ripararlo?

È stato utile?

Soluzione

La 'm' minuscola è per minuto in ora. Dovresti usare invece la 'M' maiuscola.

SimpleDateFormat sdf = new SimpleDateFormat("d/M/yyyy"); 

Vedi SimpleDateFormat JavaDoc per maggiori dettagli.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top