我想獲得當前時間在安裝該應用的Android設備上。如何獲取當前的Android設備上的時間?
我希望能夠做這樣的事..
if(//time is pass noon){
//Do something
}
else{
//do something.
}
我要告訴如果它的上午或下午。
我想獲得當前時間在安裝該應用的Android設備上。如何獲取當前的Android設備上的時間?
我希望能夠做這樣的事..
if(//time is pass noon){
//Do something
}
else{
//do something.
}
我要告訴如果它的上午或下午。
下面是一個例子,應該幫助你:
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
}
要判斷它是上午還是下午使用:
int value = today.get(Calendar.AM_PM);
或者你可以只得到一小時:
int hour = today.get(Calendar.HOUR);
然後把在if語句:
if (hour > 12)
{
// do stuff
}
else
{
// do other stuff
}
現在時間是可用
new Date()
解析,對於時間,你可以計算出,如果是設備AM或PM本地時間。
您將在這裏得到的時間在24個小時內format.So你可以做你喜歡
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");
}