0

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 different value than that was set by me manually.

Code

Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR,7);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Toast.makeText(getApplicationContext(), "Alarm worked. "+cal.HOUR+cal.MINUTE,cal.SECOND Toast.LENGTH_LONG).show(); 

Result i want

Alarm worked. 7:30:0

What i get now

Alarm worked. 10:12:13

P.S

1.I found many posts which deal with Calendar issues but couldn't find my solution.

2.The result what i am getting i.e 10:12:13 is not my current time(current date,current minute,current second) either.So i don't know why and from where these data are coming.

3.I tried using HOUR_OF_DAY instead of HOUR but nothing useful.

Solution

Toast.makeText(getApplicationContext(), "Alarm worked. "+cal.get(Calendar.HOUR)+" "+cal.get(Calendar.MINUTE)+" "+cal.get(Calendar.SECOND), Toast.LENGTH_LONG).show(); 

This worked because Calendar.HOUR,Calendar.MINUTE are constants.See the answer of @PopoFibo to get the clear picture.

Sash_KP
  • 5,551
  • 2
  • 25
  • 34

4 Answers4

1

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

Lingeshwaran
  • 579
  • 4
  • 15
  • Thanks for suggesting.Though it seems right but it didn't work in my case.Please see my edit for my solution. – Sash_KP Jan 31 '14 at 14:02
1

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

Calendar.HOUR = 10
Calendar.MINUTE = 12
Calendar.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
StoopidDonut
  • 8,547
  • 2
  • 33
  • 51
  • Perfect.Works like a charm.`Calendar.HOUR` is a constant.Got it.Thanks for your valuable help. – Sash_KP Jan 31 '14 at 14:00
  • Hey @PopoFibo could you please check [this post](http://stackoverflow.com/questions/21482969/formatting-miliseconds-to-hhmmss-format) too?Another silly problem there i guess. – Sash_KP Jan 31 '14 at 14:54
  • please see [this post](http://stackoverflow.com/questions/21508611/alarm-cancel-button-not-working-correctly) if you could please help me. – Sash_KP Feb 02 '14 at 10:06
0

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

kjurkovic
  • 4,044
  • 2
  • 23
  • 28
0

add these lines before setting time..

                Cal.set(Calendar.MONTH , month);
                Cal.set(Calendar.YEAR, year);
                Cal.set(Calendar.DAY_OF_MONTH,day);
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
  • Thanks for suggesting.But this didn't work.However i fixed it with the help of above answer.Please check my edit. – Sash_KP Jan 31 '14 at 14:01