Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calendar in java returning 12-hour format always

I am trying to set the Calendar to 24-hour format but still it is returning me the time in 12-hour format.

I have tried to set hour_of_day as well but still it is returning the same 12-hour time, tried using Gregorian as well.
Here is my code:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            datecreated = format.parse(created);
            Calendar cal = Calendar.getInstance();
            cal.setTime(datecreated);
            System.out.println(datecreated.getHours());
            cal.set(Calendar.HOUR_OF_DAY, datecreated.getHours());
            System.out.println(format.format(cal.getTime()));
            cal.add(Calendar.MINUTE, Integer.parseInt(args[4]));
            System.out.println(format.format(cal.getTime()));

In this datecreated is a Date type variable which is converted from string to data using SimpleDateFormat. Example format: "2016-06-06 17:09:29"
But the above returns "2016-06-06 05:09:29" as the value no matter what I do. Please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you need to change ‘hh’ to the ‘HH’

Here hh is for 12 hour format and HH for 24 hour format.

Change this line

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

to

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading