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,910
  • 52
  • 193
  • 240
android_king22
  • 759
  • 2
  • 20
  • 36

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,156
  • 4
  • 50
  • 75
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
  • 15,499
  • 6
  • 40
  • 50
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
  • 151,642
  • 46
  • 269
  • 291
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,539
  • 13
  • 92
  • 123