Why parsing '23:00 PM' with SimpleDateFormat(“hh:mm aa”) returns 11 a.m.?

You should be getting an exception, since "23:00 PM" is not a valid string, but Java's date/time facility is lenient by default, when handling date parsing The logic is that 23:00 PM is 12 hours after 11:00 PM, which is 11:00 AM the following day. You'll also see things like "April 31" being parsed as "May 1" (one day after April 30) If you don't want this behavior, set the lenient property to false on your SimpleDateFormat using DateFormat#setLenient(boolean) and you'll get an exception when passing in invalid date/times.

You should be getting an exception, since "23:00 PM" is not a valid string, but Java's date/time facility is lenient by default, when handling date parsing. The logic is that 23:00 PM is 12 hours after 11:00 PM, which is 11:00 AM the following day. You'll also see things like "April 31" being parsed as "May 1" (one day after April 30).

If you don't want this behavior, set the lenient property to false on your SimpleDateFormat using DateFormat#setLenient(boolean), and you'll get an exception when passing in invalid date/times.

Ohh, I've seen that flag before, and never knew that was it all about. :P ( nor care about it ) – OscarRyz Jul 20 '09 at 18:10 3 Do you now? :) – Jack Leow Jul 20 '09 at 18:11.

You want "HH:mm aa" as your format, if you will be parsing 24-hour time. Public static void main(String args) throws ParseException { SimpleDateFormat df = new SimpleDateFormat("HH:mm aa"); final Date date = df. Parse("23:00 PM"); System.out.

Println("date = " + df. Format(date)); } outputs date = 23:00 PM.

I would guess that it does something like: hours = hours % 12; to ensure that the hours are in the proper range.

23:00 PM could be thought of as 11 AM the next day. Javascript and PHP work like this pretty much but I can't speak for Java.

That's what I looks it's happening. Yeap! – OscarRyz Jul 20 '09 at 18:02.

Here are the formatting options specifed in the javadoc H Hour in day (0-23) k Hour in day (1-24) K Hour in am/pm (0-11) h Hour in am/pm (1-12) Notice that "h" would be for hours 1-12. If you want to handle 1-24, try "k". For 0-23 try "H".

But I would not expect valid results if you are putting in impossible data.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions