I am working on an android app that requires me to count down the time to Christmas by days, hours, minutes and seconds. This code must be run on an emulator later in the project.
I cannot get the time to update correctly. Sometimes I am able to have the code print out continually the exact time, but that is it. It will not update and subtract the seconds, minutes, hours and days.
When I run the code in its current condition even with try/catch, it does not update the time every second. Is there a loop that can update the time every second without causing the compiler to have too much to process out?
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(new Date(0)); /* reset */
thatDay.set(Calendar.DAY_OF_MONTH, 25);
thatDay.set(Calendar.MONTH, 11); // 0-11 so 1 less
thatDay.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
while(true)
{
long diff = thatDay.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); // % 24 not needed
print (days+""+hours+""+minutes+""+seconds+"")