1

I'm trying to make a countdown timer that will reset weekly (Mondays at 10am eastern). I've found something similar to what I'm looking for, but it doesn't reset itself; it goes into the negative. Can someone please help me get this working?

public static int SECONDS_IN_A_DAY = 24 * 60 * 60;

public String main(String[] args) {

    Calendar now = Calendar.getInstance();
    int year  = now.get(Calendar.YEAR);
    int month = now.get(Calendar.MONTH);

    Calendar reset = Calendar.getInstance();
    reset.setTime(new Date(0)); 
    reset.set(Calendar.DAY_OF_WEEK, 1); //2
    reset.set(Calendar.MONTH, month);
    reset.set(Calendar.YEAR, year);
    reset.set(Calendar.HOUR_OF_DAY, 0); //10
    reset.set(Calendar.MINUTE, 33);

    Calendar today = Calendar.getInstance();
    long diff =  reset.getTimeInMillis() - today.getTimeInMillis(); 
    long diffSec = diff / 1000;

    long days = diffSec / SECONDS_IN_A_DAY;
    long secondsDay = diffSec % SECONDS_IN_A_DAY;
    long seconds = secondsDay % 60;
    long minutes = (secondsDay / 60) % 60;
    long hours = (secondsDay / 3600);

    if (diff < 0) {
        reset.setTime(new Date(0)); 
        reset.set(Calendar.DAY_OF_WEEK, 1); //2
        reset.set(Calendar.MONTH, month);
        reset.set(Calendar.YEAR, year);
        reset.set(Calendar.HOUR_OF_DAY, 0); //10
        reset.set(Calendar.MINUTE, 33);
    }

    return "Reset in: " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds.";
}

public void onMessageReceived(MessageReceivedEvent e) {

    if(e.getMessage().getRawContent().equalsIgnoreCase(";reset")) {
        e.getChannel().sendMessage(main(null)).queue();
    }
}    

I cant figure out how to make this reset once it reaches reset time. Any help very much appreciated. I haven't coded in 5+ years.

Below is screenshot of the output if the time has passed.

https://puu.sh/w3odJ/009663b380.png

Thanks!

UPDATE: I'm finding the best method for this is to use reset.add to add 7 days to the reset calendar. Can't get the proper conditions to be met to achieve this yet though. I've tried reset.compareTo(today) as well as some other combinations which aren't working, but it's progress.

2
  • 2
    Every attempt fails... Is not a description we can help with. Read minimal reproducible example please. And I am not an android expert, but I am sure that there must be better means to trigger functionality in the future.
    – GhostCat
    Commented May 28, 2017 at 4:55
  • I can't get this to return a negative result, but if diff was negative, which would happen if the reset date was before today, then all the other values calculated would be negative, too. Commented May 28, 2017 at 6:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.