mayank mayank - 2 months ago 13
Java Question

Convert String to Date java error

My date in string format is 2016-09-17 12:12:44.
I am converting it to date object by this:

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
Date endDate = sdf1.parse(expiry);


The output is Sat Sep 17 00:12:44 GMT+05:30 2016. However the output should be:
Sat Sep 17 12:12:44 GMT+05:30 2016

Answer

As mentioned in the document H - represents 24 hours format and h - represents 12 hours format with am/pm

So the format should be yyyy-MM-dd HH:mm:ss

        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        Date endDate = sdf1.parse("2016-09-17 12:12:44");
        System.out.println(endDate.toString());