Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果日期是第二天,则运行方法_Java_Android_Java.util.date - Fatal编程技术网

Java 如果日期是第二天,则运行方法

Java 如果日期是第二天,则运行方法,java,android,java.util.date,Java,Android,Java.util.date,我想这样做: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); int i = (int) (new Date().getTime()/1000); if( ) // next day { mymethod(); } } 当

我想这样做:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

 int i = (int) (new Date().getTime()/1000);

   if(      ) // next day
     {
         mymethod();
     }
}

当系统中的日期是新的一天时,我想调用
mymethod()

建议使用
Calendar
类而不是
date
。您可以检查一天是否像这样过去:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

 int i = (int) (new Date().getTime()/1000);

   if(      ) // next day
     {
         mymethod();
     }
}
  • 在某些事件中,例如单击按钮、应用程序首次启动、将时间保存在SharedReferences中
  • 检索该值并将其与当前时间进行比较 仅举一个示例说明如何执行此操作:

    protected void onCreate(Bundle savedInstanceState){
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
    
        Calendar c = Calendar.getInstance(); 
        int currentTimeSeconds = c.get(Calendar.SECOND);
    
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int secondsPreviousDay = prefs.getInt("seconds", 0);
        if (secondsPreviousDay != 0){ //means there was an earlier set value from previously entering the activity
            //compare if more than 3600*24 = 86400 (1 day in seconds) had passed
            if (currentTimeSeconds - secondsPreviousDay > 86400){
                 mymethod();
            }
        }
        else {
            prefs.edit().putInt("seconds", currentTimeSeconds).apply();
        }
    }
    
  • 在我的代码中,我假设您希望查看从第一次开始此活动到第二次活动之间是否已经过了一天。你可以根据自己的喜好调整,但我希望这个想法是可以理解的

    我知道我可以把这两个if压缩成一个,但我只是想让它更清楚我在追求什么

    好主意

    在我的第一个活动中,我在onCreate中设置了如下内容:

     Calendar c = Calendar.getInstance();
        int currentTimeSeconds = c.get(Calendar.SECOND);
        SharedPreferences share = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edittime = share.edit();
        edittime.putInt("timevalue",currentTimeSeconds);
        edittime.commit();
    
    并在onCreate内的另一个活动中使用您的代码:

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        int secondsPreviousDay = prefs.getInt("seconds", 0);
        if (secondsPreviousDay != 0){ //means there was an earlier set value from previously entering the activity
            //compare if more than 3600*24 = 86400 (1 day in seconds) had passed
            if (currentTimeSeconds - secondsPreviousDay > 86400){
               // mymethod();
                mymethod();
    
            }
        }
        else {
            prefs.edit().putInt("seconds", currentTimeSeconds).apply();
        }
    }
    

    我很抱歉,但我仍然在学习新的一天,你是说午夜之后?那么您想检测日期是否与上次不同

      @Override
      protected void onResume() {
         SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
         int lastTimeStarted = settings.getInt("last_time_started", -1);
         Calendar calendar = Calendar.getInstance();
         int today = calendar.get(Calendar.DAY_OF_YEAR);
    
        if (today != lastTimeStarted) {
          //startSomethingOnce();
    
          SharedPreferences.Editor editor = settings.edit();
          editor.putInt("last_time_started", today);
          editor.commit();
        }
      }
    

    新的一天与什么相比?如果相隔一天,你需要参考另一个日期进行比较。这可能一点也不困难。将值存储在SharedReferences中。然后在需要时检索它,并有一个条件来确定它是否在一天后。如果我想每天只调用一次此方法,该怎么办?请注意,您只在Android Studio中编写代码。你的问题与Android Studio本身无关,因此我对其进行了编辑,然后将添加到SharedReferences中的内容放到你喜欢的地方,然后在其他地方检索,并将其值与我代码中的当前时间进行比较。。你还有其他问题吗?我的方法一天只会导致一次,不是一次又一次对吗?那就是:再加一个小东西,每次触发您的方法时,要么将新时间写入SharedReferences,要么保留某种类型的日期计数器和一个指示器,指示事件是否已在当天触发。您使用的是两种不同的SharedReferences,请参见:SharedReferences share=GetSharedReferences(“时间”,MODE\u PRIVATE);和SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(此选项);您需要相同的SharedReferences。Calendar.SECOND是否不仅仅是当前分钟的秒数?将该值与86400进行比较是没有意义的。例如,如果时间是00:01//它开始了新的一天{我的神话在我开始活动时做点什么}但是我还想一天只归纳一次mymethod(),然后,你只需要日历。day OF OF the YEAR,今天是2016年的73天,一直保持到午夜,午夜后是74天,此时,您的应用程序将启动mymethod()。这个方法一天最多调用一次。你能重写上面的代码吗,这样我就知道该怎么做了?这真是个好主意。