Calendar.set (Calendar.HOUR_OF_DAY, alarmHour); does not work. What am I doing wrong?

I set an alarm for which I accept Hour and Minutes from TextViewand AM / PM through Spinner. This is how I initialize the object Calendar:

Calendar calen = Calendar.getInstance();
calen.set(Calendar.HOUR_OF_DAY, alarmHour); //alarmHour from TextView
calen.set(Calendar.MINUTE, alarmMinute); //alarmMinute from TextView
calen.set(Calendar.SECOND, 0);
calen.set(Calendar.MILLISECOND, 0);

if(amorpm.equals("PM") //amorpm from Spinner
{
    calen.set(Calendar.AM_PM, Calendar.PM);
}
else
{
    calen.set(Calendar.AM_PM, Calendar.AM);
}

The problem is that the Hour value of this object is Calendarsometimes correct, that is, the value that the user enters in TextView(and it is always from 1 to 12). But sometimes the value is equal to the current Hour. For example, if the current time is 11:30 in the evening , and I set the alarm for 9:30 in the morning , then the Hour field is 11 . It is strange that when I change the name of an object Calendarto something else, say cal , it works. But I will not work later. What could be wrong?

Thank you for your help!

+5
source share
1 answer

I think the solution is to call calen.set(Calendar.HOUR, alarmHour);insteadcalen.set(Calendar.HOUR_OF_DAY, alarmHour);

, Calendar.HOUR_OF_DAY , AM/PM. " " - 24- , AM/PM . Java (http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html) , ​​ , :

HOUR_OF_DAY

AM_PM + HOUR

AM/PM , Calendar.HOUR, , , .

- , , . .

+12
source

All Articles