Java implements the countdown function (calculated in seconds by day, hour, minute, second), and java countdown

Source: Internet
Author: User

Java implements the countdown function (calculated in seconds by day, hour, minute, second), and java countdown

Sometimes it is required to achieve such a requirement:

The values of days, hours, minutes, And seconds are separated. The server returns only one timestamp length.

The following is my Implementation Scheme, which aims to reduce object generation and computing workload:

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; publi C 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 );} /*** initialization value ** @ param totalSeconds */private static void initData (long totalSeconds) {resetData (); if (totalSeconds> 0) {secondNotAlready = true; second = t OtalSeconds; if (second> = 60) {minuteNotAlready = true; minute = second/60; second = second % 60; if (minute> = 60) {hourNotAlready = true; hour = minute/60; minute = minute % 60; if (hour> 24) {dayNotAlready = true; day = hour/24; hour = hour % 24 ;}}}} system. out. println ("after initial formatting -->" + day + "day" + hour + "hour" + minute + "minute" + second + "second ");} private static void resetData () {day = 0; hour = 0; minute = 0; second = 0; dayNotAlready = false; hourNotAlready = false; minuteNotAlready = false; secondNotAlready = false ;} /*** calculate the change of each value **/public static void startCount () {if (secondNotAlready) {if (second> 0) {second --; 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 ("beyond the deadline -->" + day + "day" + hour + "hour" + minute + "minute" + second + "second ");}}

Summary

The above section describes how to implement the countdown function in Java (calculated in seconds by day, hour, minute, and second, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

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.