繁体   English   中英

如何计算24小时过去与否

[英]How to count that 24 Hours passed or not

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

如果要“ 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返回当前默认时区中该瞬间的文本表示形式,即在进行调用时(或创建SimpleDateFormat时)对JVM有效的时区。

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

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

如果您想进行更安全的比较,请使用Androids Calendar 它检查诸如时区等的几项内容。

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

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

您可以使用如下形式:

 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是正确的,尤其是在每天都要这样的情况下。

设置新的currentTime时将布尔值hasDayPassed设置为false,然后将currentTime与较新的时间进行比较(如果它们匹配),则为true。

祝好运

有两种情况:

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

我认为的第一个是:

//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中添加一个表示时区+/-小时的整数。 与返回时相比,您必须比较两个时区,并从新日期中减去/减去时差。

节省首选项中的时间:

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
}

PS ::虽然它不考虑时区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM