2

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{
//do something.
}

i want to tell if its am or pm.

Andro Selva
  • 53,136
  • 52
  • 190
  • 238
android_king22
  • 729
  • 2
  • 20
  • 35

4 Answers4

1

Here is an example that should help you:

Calendar today = Calendar.getInstance();
Calendar expireDate = 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:

int value = 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
}
Jack
  • 9,037
  • 4
  • 46
  • 74
1

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

final Calendar cld = Calendar.getInstance();

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

    }else{
    //
    } 
Rasel
  • 16,321
  • 6
  • 41
  • 51
1
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");
}
maerics
  • 143,080
  • 41
  • 260
  • 285
0

The current time is available with

new Date()

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

Manfred Moser
  • 29,012
  • 13
  • 90
  • 122