Android timer and Time Calculator Implementation Method

Source: Internet
Author: User

Requirement: The default value is "00:00:00". The timer starts after you click the start button, as shown in 10:28:34. Click Stop to stop timing.
Problem: the Calendar DateFormat method is used. If the time zone is not set, the hour obtained is the local time zone (the UTC + 8 zone is 8 ), the time obtained from the GMT Standard Time Zone is 12 hours (12:00:00). The 24-hour format is invalid.
The addition or subtraction of hours at the start time is invalid, and the timer automatically jumps up only to 12 hours. The default status 00:00:00 starts timing.
You can only write one method to convert the Time Format String Based on the number of seconds after trying different time setting methods. After testing, it is okay. The double digits can only display 99 hours as the maximum value, if you need more hours, you need to change the method.
In addition, the number of hours cannot be infinitely large. If the long data type length is exceeded, it will become a negative number and an exception will occur.

Display Effect:

Test class:

Copy codeThe Code is as follows: public class TestTime {
Public static void main (String [] args ){
TestTime tt = new TestTime ();
Tt. showTimeCount (99*3600000 + 75*1000 );
}

// Time counter, which can only be up to 99 hours. You need to change the time counter if you need more hours.
Public String showTimeCount (long time ){
System. out. println ("time =" + time );
If (time >=360000000 ){
Return "00:00:00 ";
}
String timeCount = "";
Long hourc = time/3600000;
String hour = "0" + hourc;
System. out. println ("hour =" + hour );
Hour = hour. substring (hour. length ()-2, hour. length ());
System. out. println ("hour2 =" + hour );

Long minuec = (time-hourc x 3600000)/(60000 );
String minue = "0" + minuec;
System. out. println ("minue =" + minue );
Minue = minue. substring (minue. length ()-2, minue. length ());
System. out. println ("minue2 =" + minue );

Long secc = (time-hourc * 3600000-minuec * 60000)/1000;
String sec = "0" + secc;
System. out. println ("sec =" + sec );
Sec = sec. substring (sec. length ()-2, sec. length ());
System. out. println ("sec2 =" + sec );
TimeCount = hour + ":" + minue + ":" + sec;
System. out. println ("timeCount =" + timeCount );
Return timeCount;
}

}

Example:

Copy codeThe Code is as follows: // time counter, which can only be up to 99 hours. If you need more hours, change the method.
Public String showTimeCount (long time ){
If (time >=360000000 ){
Return "00:00:00 ";
}
String timeCount = "";
Long hourc = time/3600000;
String hour = "0" + hourc;
Hour = hour. substring (hour. length ()-2, hour. length ());

Long minuec = (time-hourc x 3600000)/(60000 );
String minue = "0" + minuec;
Minue = minue. substring (minue. length ()-2, minue. length ());

Long secc = (time-hourc * 3600000-minuec * 60000)/1000;
String sec = "0" + secc;
Sec = sec. substring (sec. length ()-2, sec. length ());
TimeCount = hour + ":" + minue + ":" + sec;
Return timeCount;
}

Private Handler stepTimeHandler;
Private Runnable mTicker;
Long startTime = 0;

// Start button
Class startBtnListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Button B = (Button) v;
String buttonText = B. getText (). toString ();
If ("Start". equalsIgnoreCase (buttonText )){
B. setText ("Stop ");
// Start time resetting
StepTimeTV. setText ("00:00:00 ");
StepTimeHandler = new Handler ();
StartTime = System. currentTimeMillis ();
MTicker = new Runnable (){
Public void run (){
String content = showTimeCount (System. currentTimeMillis ()-startTime );
StepTimeTV. setText (content );

Long now = SystemClock. uptimeMillis ();
Long next = now + (1000-now % 1000 );
StepTimeHandler. postAtTime (mTicker, next );
}
};
// Start the timer thread and update regularly
MTicker. run ();
} Else {
B. setText ("Start ");
// Stop timing Remove any pending posts of Runnable r that are in the message queue.
StepTimeHandler. removeCallbacks (mTicker );
}
}
}

Test code in time format:

Copy codeThe Code is as follows: // the start button uses the Calendar time setting method, and the hour cannot be normally displayed as 0
Class startBtnListener implements OnClickListener {
@ Override
Public void onClick (View v ){
Button B = (Button) v;
String buttonText = B. getText (). toString ();
If ("Start". equalsIgnoreCase (buttonText )){
B. setText ("Stop ");
// Start time resetting
StepTimeTV. setText ("00:00:00 ");
If (mCalendar = null ){
MCalendar = Calendar. getInstance ();
TimeZone tz = TimeZone. getTimeZone ("GMT"); // GMT + 8
MCalendar. setTimeZone (tz );
MCalendar. get (Calendar. HOUR_OF_DAY); // in 24-hour format
}
StepTimeHandler = new Handler ();
// System. uptimeMillis () // records the number of milliseconds that have elapsed since the machine was started. When the System enters deep sleep, this timer stops
// System. currentTimeMillis () // returns the number of milliseconds from January 1, January 1, 1970 to the present, usually used to set the date and time
// System. elapsedRealtime () // returns the number of milliseconds that have elapsed since the machine was started, including the System sleep time. This method is not available in the api.
// Obtain the local time zone. The local time is related to the time zone. After GMT is set, the local time is always more than 12 hours.
StartTime = System. currentTimeMillis (); // 12*3600000-36*3600000 lost or 12 hours added
MTicker = new Runnable (){
Public void run (){
// The offset date is in the format of January 1, 1970, and cannot appear at 00:00:00.
Long showTime = System. currentTimeMillis ()-startTime;
Log. I (TAG, showTime + "");
MCalendar. setTimeInMillis (showTime + 13*3600000 + 1000 );
String content = (String) DateFormat. format (mFormat, mCalendar );
StepTimeTV. setText (content );

Long now = SystemClock. uptimeMillis ();
Long next = now + (1000-now % 1000 );
StepTimeHandler. postAtTime (mTicker, next );
}
};
// Start the timer thread and update regularly
MTicker. run ();
} Else {
B. setText ("Start ");
// Stop timing Remove any pending posts of Runnable r that are in the message queue.
StepTimeHandler. removeCallbacks (mTicker );
}
}
}

Private Handler stepTimeHandler;
Calendar mCalendar;
String mFormat = "yyyy-MM-dd hh: mm: ss"; // yyyy-MM-dd
Long startTime = 0;
Private Runnable mTicker;

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.