Simple date formatting giving wrong time

后端 未结 4 1759
暗喜
暗喜 2021-01-20 05:57

I am trying to parse a String to a Date and it giving me right date where as time is wrong.

SimpleDateFormat formatter = new SimpleDateFormat(\"yyyy-MM-dd HH         


        
4条回答
  •  遇见更好的自我
    2021-01-20 06:34

    It seems the problem was that your pattern String specified am/pm, but was using uppercase H's for the hour characters. These indicate a 24-hour clock, which obviously doesn't use am/pm. Change the hour characters to lowercase h's, which indicate the hour in am/pm (0-11).

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm aaa");
    

    The docs for SimpleDateFormat explain the various acceptable pattern characters.

提交回复
热议问题