There are times when you are asked to implement such a requirement:
The values for days, hours, minutes, and seconds are separated, and the server side returns only a timestamp length.
Here is my implementation to reduce the number of objects generated, reduce the amount of computing:
Import Java.util.Timer;
Import Java.util.TimerTask;
public class Countdownutils {private static long day = 0;
private static long hour = 0;
private static long minute = 0;
private static long second = 0;
private static Boolean daynotalready = false;
private static Boolean hournotalready = false;
private static Boolean minutenotalready = false;
private static Boolean secondnotalready = false;
public static void Main (string[] args) {Long totalseconds = 60 * 60 * 24 * 34 + 21;
InitData (totalseconds);
New Timer (). Schedule (new TimerTask () {public void run () {if (Secondnotalready) {startcount ();
else {cancel ();
}}, 0, 1000);
/** * Initialize Assignment * * @param totalseconds * * private static void InitData (Long totalseconds) {resetdata ();
if (TotalSeconds > 0) {Secondnotalready = true;
second = totalseconds;
if (second >=) {Minutenotalready = true;
minute = SECOND/60;
Second = second% 60; if (minute >=) {Hournotalready = true;
hour = MINUTE/60;
minute = minute% 60;
if (Hour >) {Daynotalready = true;
Day = hour/24;
Hour = hour% 24;
}}} System.out.println ("Initially formatted-->" + Day + "days" + Hour + "hours" + Minute + "minutes" + second + "seconds");
private static void ResetData () {day = 0;
hour = 0;
minute = 0;
Second = 0;
Daynotalready = false;
Hournotalready = false;
Minutenotalready = false;
Secondnotalready = false; /** * Calculate the changes of each value */public static void Startcount () {if (Secondnotalready) {if (Second > 0) {SE
cond--;
if (second = = 0 &&!minutenotalready) {secondnotalready = false;
} else {if (Minutenotalready) {if (minute > 0) {minute--;
Second = 59;
if (minute = = 0 &&!hournotalready) {minutenotalready = false; } else {if (Hournotalready) {if (hour >
0) {hour--;
minute = 59;
Second = 59;
if (hour = = 0 &&!daynotalready) {hournotalready = false;
} else {if (daynotalready) {day--;
hour = 23;
minute = 59;
Second = 59;
if (day = = 0) {Daynotalready = false; }}}}}}}}system.out.println("distance due date and-->" + Day + "days" + Hour + "hour" + M
Inute + "Minutes" + second + "seconds");
}
}