有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android从今天起30天

我需要我的应用VIP功能从今天起30天到期,我将在应用程序配置中存储当前日期。如何检查VIP功能是否已过期?我不介意用户是否将时钟倒转,应用程序是否正常工作

当用户激活VIP功能时

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

// in want to also set StartDate and EndDate

然后验证mCurrentUser endDate中保存的日期是否与今天一致

// set user to no VIP! like

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

共 (2) 个答案

  1. # 1 楼答案

    您可以通过以下方法从给定日期起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,则时间已过期

  2. # 2 楼答案

    如果你想让它在30天后的第二天到期,他启动了vip,你可以使用这个:

    要启动计时器:

    SharedPreferences.editor editor1= getSharedPreferences("vipexpire", MODE_PRIVATE).edit();
    
                        editor1.putInt("starttime", (int) System.currentTimeMillis() / 1000);
                        editor1.apply();
    

    在创建mainActivity时,要检查用户每次打开应用程序的时间是否已过:

    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
    }
    

    在服务器上存储时间戳时,或者如果你想在应用程序中显示准确的倒计时,这种方法也很好