Pergunta

so if print 01/18/2013 i get JUNE 01, 2014 or if i print 08/18/2013 i get JUNE 08, 2014 What should i change to get correct output in English format date?

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        Date givenDate = formatter.parse(s);
        SimpleDateFormat formatterprint = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
        System.out.println(formatterprint.format(givenDate).toUpperCase());
Foi útil?

Solução

The pattern used to parse the date is dd,MM,yyyy, which means day followed by a comma, followed by month, followed by a comma, followed by year.

You enter 08/18/2013. 08 is a valid day, but 18 isn't a valid month. And you should enter commas, not slashes.

Since you enter 18/2013 as the month, it's interpreted as the 18th month of 2013, which corresponds to the 6th month of 2014 (since years, well, have only 12 months): June.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top