【问题标题】:How to count that 24 Hours passed or not如何计算 24 小时过去与否
【发布时间】:2015-08-27 08:06:32
【问题描述】:

我面临一个问题,即我在按钮单击事件后面的共享首选项中保存当前设备时间。例如:日期(2015 年 8 月 27 日)上午 11:05。 之后,当用户进行相同的活动时,我必须根据项目要求在接下来的 24 小时内更改布局文件,直到(2015 年 8 月 28 日上午 11:05)。但是我面临的问题是,如果从单击按钮的时间(在这种情况下是上午 11:05)开始是否已经过去了 24 小时,我怎么能得到它。任何形式的帮助将不胜感激。谢谢。

【问题讨论】:

  • 只需在共享偏好中节省时间,然后当您回来时将节省的时间与当前时间进行比较。简单..!!
  • 答案真的取决于您的意思是“当地时间上午 11:05 后”还是“24 小时后”?
  • 仅供参考:在我之前的评论中,“当地时间”受夏令时和旅行(汽车、飞机、船、自行车等)到不同时区的影响。跨度>

标签: java android time


【解决方案1】:

如果您想要“24 小时后”,请使用纯 UTC 时间。最简单的是使用原始的毫秒。

// Save current time
long savedMillis = System.currentTimeMillis();

// Check time elapsed
if (System.currentTimeMillis() >= savedMillis + 24 * 60 * 60 * 1000) {
    // time has elapsed
}

如果您想“在明天的同一时间之后”,您必须记住时区,或者直接转换为未分区的文本。

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

// Save current time
String savedTime = df.format(new Date()); // Applies local time zone 1

// Check time elapsed
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(savedTime)); // Applies local time zone 2
cal.add(Calendar.DAY_OF_MONTH, 1); // Adjusts for DST in local time zone 2
if (System.currentTimeMillis() >= cal.getTimeInMillis()) {
    // time has elapsed
}

说明

new Date() 以 UTC 时间返回一个瞬间。
df.format 返回 当前默认时区 中该瞬间的文本表示,即在 JVM 生效时的时区调用(或者更确切地说是在创建 SimpleDateFormat 时)。

稍后,df.parse 将再次使用 当前默认时区 将该文本解析回 UTC 时间,但这可能与调用 df.format 时使用的时区不同.
cal.setTimeCalendar 更新为 UTC 时间的瞬间。
cal.addCalendar 更新为 1 天后的同一时间,调整为 当前默认时区中的 DST,如果需要。
cal.getTimeInMillis() 是本地时区中保存时间后 1 天的同一时间的 UTC 毫秒。

【讨论】:

    【解决方案2】:

    您通常以毫秒为单位比较时间。只需检查两次之间的差异是否大于 24*60*60*1000

    如果您想要更安全的比较,请使用 Androids Calendar。它会检查时区等多项内容。

    【讨论】:

    • 在没有时区/夏令时问题/闰年出现的情况下工作。
    • @RobAu 正确,如果他刚从纽约飞往洛杉矶,情况会变得更加复杂。是“当地时间上午 11:05 后”还是“24 小时后”?
    【解决方案3】:

    每当用户返回时,只需将当前时间与您在共享首选项中保存的时间进行比较。如果差异是 24 小时或更长时间,请执行您想做的任何事情。

    最好知道您尝试在哪种环境/编程语言中执行此操作

    【讨论】:

    • 查看标签:javaandroid
    • 在这种情况下,这篇文章可能会有所帮助:java中时间的比较:stackoverflow.com/questions/2309558/time-comparison
    • 纯时间解决方案行不通。如果存储的时间是“11:05”(没有日期)并且下一次运行是“10:45”,那么是明天(24 小时)。类似的是今天晚些时候的“11:48”,还是稍后的某个日期?
    • 在这种情况下,您必须先比较日期,然后再比较时间(如有必要)。我没发现问题?
    【解决方案4】:

    你可以这样使用:

     new CountDownTimer(30000, 1000) {
    
         public void onTick(long millisUntilFinished) {
             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
         }
    
         public void onFinish() {
             mTextField.setText("done!");
         }
      }.start();
    

    -http://developer.android.com/reference/android/os/CountDownTimer.html

    但我认为 Mamata 是对的,尤其是如果它每天都会出现的话。

    有一个布尔值 h​​asDayPassed,当设置新的 currentTime 时设置为 false,然后当 currentTime 与较新的时间进行比较时,如果它们匹配,则为 true。

    祝你好运

    【讨论】:

    • 这是 30 秒。 24 小时包括很多事情,例如系统重启、应用程序因电池电量不足而强制关闭等。所以理想的方法是节省偏好时间。它还可以减少电池消耗。
    【解决方案5】:

    有两种情况:

    • 第一个是不考虑时区:在这里您可以通过多种方式进行操作,

    我首先想到的是:

    //DateFormat is used formatting the Date with the sintax you like
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date today = Calendar.getInstance().getTime();
    //save date in SharedPreferences using
    String dateString = df.format(today);
    sp.putString("date", dateString);
    

    当你必须取回它时:

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date;
    if(preferences.contains("date")){
        String dateString = preferences.getString("date", "");
        if(dateString != ""){
            date = df.parse(dateString);
            if(new Date() >= date.addDays(1)){
                //a day is passed
            } else{
                //not passed
            }
        }else{
            //case date wrongly stored
        }
    } else{
        //no date stored
    }
    
    • 如果您必须考虑时区,请使用TimeZone 类在sharedPref 中添加一个表示时区+/- 小时的int。 与取回它相比,您必须比较两个时区并添加/减去新日期的差异。

    【讨论】:

      【解决方案6】:

      节省偏好时间:

      Editor editor=mSharedPreferences.edit(); //mSharedPreferences is instance of your SharedPreferences
      editor.putLong("time",System.currentTimeMillis());
      editor.commit();
      

      随时检查:

      if(System.currentTimeMillis()-mSharedPreferences.getLong("time",0)<24*60*60*1000){
      //with in a day
      }else{
      //greater than a day
      }
      

      P.S.:虽然它不考虑时区。

      【讨论】:

      • 哦...非常感谢@Seshu
      猜你喜欢
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2018-10-16
      • 2020-01-08
      相关资源
      最近更新 更多