Adjust the date using Calendar. AM-PM error

2

I want to create a Calendar object, but how can I take a given hour and minutes and adjust the date?

Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR,hora); //valores tomados de un TimePickerDialog
c.set(Calendar.MINUTE,minutos); //valores tomados de un TimePickerDialog

To check that it is ok I write it in the Logcat. What happens is that when I put for example the: 5:30 am when using

c.getTime().toString()

shows me: Fri Mar 30 17:30:00 GMT-04:00 2018 or is inverted from am to pm and vice versa. What can cause this? Also, if it's 4:30 p.m. on Saturday and I'll set it to 4:15 p.m., I'm supposed to get 4:15 p.m. but not Sunday? But it shows me: 16:15 on Saturday. I guess it's because of AM and PM

    
asked by Andry_UCI 31.03.2018 в 00:57
source

2 answers

4

Use Calendar.HOUR_OF_DAY instead of Calendar.HOUR , for example:

c.set(Calendar.HOUR_OF_DAY,hora);
  

Calendar.HOUR_OF_DAY is used to define a format of 24   hours.

in this way defining 5:30 would you get:

Fri Mar 30 05:30:00 GMT-04:00 2018

Another example, if you define 16:15

Fri Mar 30 16:15:00 GMT-04:00 2018
    
answered by 31.03.2018 / 01:17
source
1

In Calendar you must use HOUR_OF_DAY to set the time, in 24 hour format:

c.set(Calendar.HOUR_OF_DAY,hora); 

If you are using SimpleDateFormat, to display the time, eg 2:30 pm:

new SimpleDateFormat("HH:mm")    //14:30
new SimpleDateFormat("hh:mm")    // 2:30
new SimpleDateFormat("hh:mm a")  // 2:30 PM
    
answered by 31.03.2018 в 01:52