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.
>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");