0

Logic in mainMethod:

String todaydate=DateUtils.todayDateinToString();
if ( !todaydate.equals(localdate.get())) {
    long currentMillis = System.currentTimeMillis();
    if(currentMillis >= appProperties.nextCallMillis.get() ) {
        localdate.set(currentMillis + 86400000);
         callmethod();
    }
    appProperties.barCodeCallDate.set(DateUtils.todayDateinToString());
}
else{
    appProperties.nextCallMillis.clear();
}

DateUtils.java

public static  String todayDateinToString() {
    String today = null;
    try {
        Date date = Calendar.getInstance().getTime();

        // Display a date in day, month, year format
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        today = formatter.format(date);
        System.out.println("Today : " + today);
    } catch (Exception e) {
        e.printStackTrace();

    }
    return today;
}

i wrote this code to execute each day once after 24 hour interval.

Actual: its executing each day only once.

Expected: it should execute each day after 24 hour. For example i call method today at 11 am then it shouldn't call tomorrow before 11 am. It should call after 11 am. If I try to call day after tomorrow it should call before 11 am because that difference for next call is 24 hour.

please suggest me i am storing two date and nextCallMillis in locally .

James Z
  • 11,838
  • 10
  • 25
  • 41
Joan
  • 61
  • 2
  • 9
  • are you doing this in android? – karan Oct 26 '18 at 12:59
  • yes in android i have to do it @KaranMer – Joan Oct 26 '18 at 13:01
  • you can use alarmmanager for it, check this https://stackoverflow.com/questions/3052149/using-alarmmanager-to-start-a-service-at-specific-time – karan Oct 26 '18 at 13:02
  • test case we dont have to use Alarm Manager @KaranMer – Joan Oct 26 '18 at 13:04
  • You're making it quite complex with the millisecond usage etc. Just take the date you executed it last time, and calculate for example total seconds between the dates. No need for different handling on what day it is etc. If it's over 24 hours, then it's ok. – James Z Oct 26 '18 at 16:09
  • @JamesZ can you edit and tell me how we can do this – Joan Oct 27 '18 at 04:26

0 Answers0