Android - Find out what hour of day it is

bobby123 picture bobby123 · Mar 7, 2011 · Viewed 16k times · Source

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

Answer

Matthew Willis picture Matthew Willis · Mar 7, 2011

Try:

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