【问题标题】:Android 30 Days From Todays DateAndroid 从今天起 30 天
【发布时间】:2016-09-11 00:46:44
【问题描述】:

我需要我的应用程序 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();

【问题讨论】:

  • 您在使用丰富的日期/时间处理类和可用库时做了哪些尝试?我们不是代码编写服务。
  • 我希望看到您将服务器时区的时间戳连同用户名一起写入数据库。这避免了用户更改本地时钟的问题。
  • 例如System.currentTimeMillis() + TimeUnit.DAYS.toMillis(30) 然后检查当前时间。这正好是 30 * 24 小时,因此它可能与通常在 00:00 结束的“30 天内”略有不同。

标签: java android datetime parse-platform


【解决方案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.
}

如果为真,则时间已过。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      如果你想让它在他开始 vip 的 30 天的第二秒过期,你可以使用这个:

      启动计时器:

      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
      }
      

      在将时间戳存储在服务器上或希望在应用中显示准确的倒计时时也可以使用此方法!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多