Pergunta

I'm working with the following snippet which takes a datetime as a string, then creates a Date object from it:

Date currentDate = new Date();
SimpleDateFormat f = new SimpleDateFormat("EEEE, MMMM d 'at' h:mm a");

currentDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2014-03-06 12:59:01");
System.out.println(f.format(currentDate));

The last two lines give an output of "Thursday, March 6 at 12:59 AM", but it should be PM. What is causing this? It only happens for the noontime am/pm switch. For example if the time being parsed was ("2014-03-06 00:59:01") the output is correctly "Thursday, March 6 at 12:59 AM". Thanks in advance for your help!

Foi útil?

Solução

This line

currentDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2014-03-06 12:59:01");

contains hh which is the 1-12 hour format. Try using capital HH, which uses 0-23 format.

"yyyy-MM-dd HH:mm:ss"

Output:

Thursday, March 6 at 12:59 PM

The date formatting symbols and descriptions are in the SimpleDateFormat javadocs.

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