Android今天30天后的日子

时间:2016-05-15 01:01:21

标签: java android datetime parse-platform

我需要我的应用程序VIP功能从今天起30天后到期,我会将当前日期存储在应用程序配置中。我如何检查VIP功能是否已过期?我不介意用户是否更改了时钟并且应用程序正常运行。

当用户激活VIP功能时

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

// in want to also set StartDate and EndDate

然后验证mCurrentUser endDate中保存的日期是否为今天的igual

// set user to no VIP! like

mCurrentUser.setNoVip();
mCurrentUser.SaveInBackgroud();

3 个答案:

答案 0 :(得分:3)

您可以通过以下方法从指定日期起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.
}

如果为真,则时间已过期。

答案 1 :(得分:1)

在这种情况下,您不应该在本地存储此类信息。而是在Users表中添加字段“activationDate”。然后查询它,然后在本地进行计算。

答案 2 :(得分:0)

如果你希望它在30d内完全过期,他就开始了vip,你可以使用它:

启动计时器:

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
}

在服务器上存储时间戳或者想要在应用中显示准确的倒计时时,此方法也很不错!

相关问题