开发者

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 pa开发者_开发百科ss noon){

//Do something


}
else{
//do something.
}

i want to tell if its am or pm.


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
}


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{
    //
    } 


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");
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜