Skip to content Skip to sidebar Skip to footer

How To Get The Time On The Current Android Device?

I would like to get the current time on the android device with the app installed. I want to be able to do something like this.. if(//time is pass noon){ //Do something } else{

Solution 1:

Here is an example that should help you:

Calendartoday= Calendar.getInstance();
CalendarexpireDate= Calendar.getInstance();

expireDate.set(2011, Calendar.AUGUST, 12);

if (today.compareTo(expireDate) == 0 || today.compareTo(expireDate) == 1)

{
// expired - please purchase app

}
else
{
// do some stuff
}

To tell if it is AM or PM use:

intvalue = today.get(Calendar.AM_PM);

Or you could just get the hour:

int hour = today.get(Calendar.HOUR);

And then put that in an if statement:

if (hour > 12)
{

//do stuff

}
else
{
//do other stuff
}

Solution 2:

You will get time here in 24 hrs format.So you can do as you like

finalCalendarcld= Calendar.getInstance();

            inttime= cld.get(Calendar.HOUR_OF_DAY);
    if(time>12){
    //noon

    }else{
    //
    } 

Solution 3:

Calendar now = Calendar.getInstance();
if (now.get(Calendar.AM_PM) == Calendar.AM) {
  System.out.println("AM: Before noon");
} else { // == Calendar.PM
  System.out.println("PM: After noon");
}

Solution 4:

The current time is available with

newDate()

Parsing that for the time you can figure out if is AM or PM local time for the device.

Post a Comment for "How To Get The Time On The Current Android Device?"