Pergunta

I am facing a problem with SimpleDateFormat . when i tried to execute the program im getting output of 01-01-2014 20:18:18 insted of 10-01-2014 20:18:18 . I'm posting my code blow. Please help me find mistakes in my code .

    import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class ValidDate {

    public static void main(String[] args) {
        ValidDate vd = new ValidDate();
        vd.processDate();       

}


private  void processDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-yyyy HH:mm:ss");
        try {

            Date dt = sdf.parse("10-01-2014 20:18:18");


            System.out.println(sdf.format(dt));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

Thank you.

Foi útil?

Solução

Use dd for day in month. DD is for day in year. Day in year apparently has priority over month, so the value of 01 for Day of year overwrites the 10 for month when parsing.

Outras dicas

Change DD -> dd

SimpleDateFormat sdf = new SimpleDateFormat("MM-DD-yyyy HH:mm:ss");

to

SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top