Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Find out what hour of day it is

I want to use and alarmManager that sets a repeating alarm to go off on the hour, every hour. I know how to set a repeating alarm every hour but not how to actually set it from the top of the hour, I need to know this value for the 'whatTime' variable below.

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, whatTime, 1*60*60*1000, operation);

Also I want to be able to set a flag that for e.g. - if the time happens to be between 4 and 8 in the daytime, perform some operations, otherwise don't bother.
So I really need to know how to find out the hour of the day, can anyone tell me how to do this? Many thanks

like image 845
bobby123 Avatar asked Mar 07 '11 18:03

bobby123


People also ask

How do I get the hour of the day on my Android?

int hour = Calendar. getInstance(). get(Calendar. HOUR_OF_DAY);

Which view is used to display the date and time in Android application?

text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.


1 Answers

Try:

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
  • Calendar.HOUR_OF_DAY gives you the 24-hour time.
  • Calendar.HOUR gives you the 12-hour time.
like image 192
Matthew Willis Avatar answered Nov 14 '22 03:11

Matthew Willis