How do I know if I got past midnight or a new day started? [closed]

0

I need to make a if for when it is past midnight, but how can I get this information even with my cell phone turned off?

I'm currently trying this:

date_current = simpleDateFormat.parse(strDate);
if (date_current.getTime() == 0){
    dados.setmeianoite(true);
}

But it is after midnight while the cell phone is in the background or it does not go down in if , consequently it does not do what I want.

What I need to know is if it turns around midnight and it's not midnight.

    
asked by anonymous 26.10.2017 / 14:20

2 answers

2

date_current.getTime() == 0 will only return true when it is midnight on January 1, 1970 (epoch) .

The simplest way to check the time is using a Calendar.

date_current = simpleDateFormat.parse(strDate);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date_current);

if(calendar.get(Calendar.HOUR_OF_DAY) == 0) {
  dados.setmeianoite(true);
}
    
26.10.2017 / 15:14
2

I do not know Java or Android very well, but I think it's a matter of algorithm here.

To get started, see Leonardo Lima's answer . Your program compares the current date-time with a specific date, not midnight the day you are.

But there is one more thing missing. Your program runs from time to time, right? What I suggest:

  • Each time the relevant snippet is executed, save the datetime at which the snippet was executed in your application data.
  • In this section, compare the current date with the date the program was last run.

You do not have to take the time into consideration. If the current date and last execution date are different, then the day has "turned" since the last time the program ran.

    
26.10.2017 / 15:19