2016-05-15 87 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”。然后进行查询,然后在本地进行计算。