Java implementation Countdown (counting days, hours, minutes, seconds) __java

Source: Internet
Author: User

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 &GT
								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");
 }

}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.