I’m attempting to parse the following string into a date object: 9/14/2012 9:50:56 PM
I’m using the following format:
DateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss a");
formatter.setTimeZone(TimeZone.getTimeZone("America/New_York"));
But I keep getting the following date: Fri Sep 14 06:50:56 PDT 2012
I seem to be off by 12 hours (after accounting for the time change). However when I parse the following string: 9/14/2012 1:00:00 AM – I get the right date object: Thu Sep 13 22:00:00 PDT 2012
What I am doing wrong?
if your date is in am/pm format, you should use
hh, instead ofHHfor hours. See the reference: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.htmlWhat happens here is the
9is treated as 09 hours in 24 hour format, which is 9 am, so your date is correctly pushed back 3 hours to make it 6 am. With the second date 1 am is 01 hours, and the date is correct.