Search code examples
javasimpledateformat

Same milliseconds for two different times in java


I need to convert a 24 hour date format for a given string into milliseconds. But I get the same milliseconds for 00:10:00 and 12:10:00. This is my code sample; please assist me.

DateFormat formate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 

formate.parse("2013-10-31 12:10:00").getTime(); //  return 1383158400000

formate.parse("2013-10-31 00:10:00").getTime();  // return 1383158400000

I am using 24 hours format, but I get the same result for 2 different times. Where is the mistake? Please help me to find out the problem.


Solution

  • You are using the 12-hour hh format; use the 24-hour HH (capitalized) format.

    DateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    

    H Hour in day (0-23) Number 0

    h Hour in am/pm (1-12) Number 12