2016-05-15 190 views
-1

我需要我的應用程序VIP功能從今天開始過30天,我會將當前日期存儲在應用程序配置中。我如何檢查VIP功能是否過期?我不介意用戶是否改變了時鐘並且應用程序正常工作。Android從今天開始的30天日期

當用戶激活VIP功能

mCurrentUser.setVip(); 
mCurrentUser.SaveInBackgroud(); 

// in want to also set StartDate and EndDate 

然後覈實,如果保存在mCurrentUser結束日期的日期是igual今天

// set user to no VIP! like 

mCurrentUser.setNoVip(); 
mCurrentUser.SaveInBackgroud(); 
+2

您使用豐富的日期/時間處理類和庫可以獲得什麼樣的嘗試?我們不是代碼寫作服務。 – hexafraction

+0

我希望看到您在服務器的時區和用戶名中向數據庫寫入時間戳。這避免了用戶改變他的本地時鐘的問題。 –

+0

例如'System.currentTimeMillis()+ TimeUnit.DAYS.toMillis(30)',然後檢查當前時間。這正是30 * 24小時,所以它可以被看作與「30天內」有點不同,這通常在00:00時纔會有所不同。 – zapl

回答

2

您可以通過以下後30天獲取日期從給定日期方法。

Calendar c = Calendar.getInstance(); 
    c.setTime(startDate); 
    c.add(Calendar.DATE, 30); 
Date expDate = c.getTime(); 

之後,您可以比較兩個日期。

if(startDate.after(expiate)){ 
    // Your time expired do your logic here. 
} 

如果爲true,則表示時間已過。

0

如果你想讓它正好到期的30D第二,他開始貴賓,您可以使用此:

啓動定時器:

SharedPreferences.editor editor1= getSharedPreferences("vipexpire", MODE_PRIVATE).edit(); 

        editor1.putInt("starttime", (int) System.currentTimeMillis()/1000); 
        editor1.apply(); 
在mainActivity中的onCreate

,檢查每次用戶在時間結束時打開應用程序:

SharedPreferences vipTimer= getSharedPreferences("vipexpire", MODE_PRIVATE); 
int startTime= vipTimer.getInt("starttime", 0); 
int currentTime = System.currentTimeMillis()/1000; 
int timeofVip= currentTime - startTime; //calculate the time of his VIP-being time 
if (timeofVip>= 2592000) //2592000 is 30 days in seconds 
{ 
//30 days over, update user to non-vip 
} 
0

在這種情況下,您不應該在本地存儲這些信息。相反,請在用戶表中添加字段「activationDate」。然後進行查詢,然後在本地進行計算。