Skip to content Skip to sidebar Skip to footer

Accessing Hour,minute And Second Using Calendar Class

I am trying to set an alarm on a particular day and time.So setting hour and minute using Calendar.But when i try to access the hour which is set in Calendar using cal.set,i get a

Solution 1:

The problem is in your Toast, you are printing the constants of the Calendar class:

Calendar.HOUR = 10Calendar.MINUTE = 12Calendar.SECOND = 13

Instead, get the value at their respective indexes - like for HOUR it would be cal.get(Calendar.HOUR)

System.out.println(cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));

Would give (correct output):

7:30:0

And, System.out.println(Calendar.HOUR + ":" + Calendar.MINUTE + ":" + Calendar.SECOND);

Would give (incorrect output):

10:12:13

Solution 2:

Date s = cal.getTime();SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss a");String t = sdf.format(s); Toast.makeText(this, t, Toast.LENGTH_SHORT).show(); for more ref. see http://www.asbhtechsolutions.com/android-tutorial/1-android-get-current-time-and-date

Solution 3:

can you try with:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 30);

and then toast it

Solution 4:

add these lines before setting time..

                Cal.set(Calendar.MONTH , month);
                Cal.set(Calendar.YEAR, year);
                Cal.set(Calendar.DAY_OF_MONTH,day);

Post a Comment for "Accessing Hour,minute And Second Using Calendar Class"