Requirements: Default to "00:00:00", click the Start button when the 0 start time, appear as 10:28:34. Stop timing when the click stops.
Problem: Using the Calendar DateFormat method, do not set the time zone gets to the hour is the local time zone (East Eight is 8), set to GMT Standard Time zone to get the time is 12 hours (12:00:00), set 24 hour system is invalid.
The start time plus minus the various hours are not valid, and the timing can be automatically jumped up to 12 hours, always can not appear the default state 00:00:00 start timing effect.
Try a variety of time settings method is not valid after you can only write a time format based on the number of seconds to convert the string method, after testing is no problem, two digits can only show 99 hours for the maximum, such as the need for more hours to change the method.
The other hours can not be infinitely large, more than long data type length will become negative, there will be an exception.
Display effect:
Test class:
Copy Code code as follows:
public class Testtime {
public static void Main (string[] args) {
Testtime tt = new Testtime ();
Tt.showtimecount (99*3600000+75*1000);
}
Time counter, up to 99 hours, if need more hours to change the method
Public String Showtimecount (long) {
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*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;
}
}
Actual example:
Copy Code code as follows:
Time counter, up to 99 hours, if need more hours to change the method
Public String Showtimecount (long) {
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*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");
Zero Start timing
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 timer thread, update regularly
Mticker.run ();
}else{
B.settext ("Start");
Stop the timer Remove any pending posts of Runnable R this are in the message queue.
Steptimehandler.removecallbacks (Mticker);
}
}
}
To test your code in a time-formatted fashion:
Copy Code code as follows:
The Start button is set by the calendar time so that the hour is not displayed properly 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");
Zero Start timing
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);//24-hour system
}
Steptimehandler = new Handler ();
System.uptimemillis ()//record the number of milliseconds from the start of the machine to the present, and the timer will stop when the system goes into deep sleep
System.currenttimemillis ()//Returns the number of milliseconds since January 1, 1970, usually used to set the date and time
System.elapsedrealtime ()//Returns the number of milliseconds from the start of the machine to the present, including the time of deep sleep in the system, which is not in the API
Direct access to the local time zone time, local times and time zones, set GMT always more than 12 hours
StartTime = System.currenttimemillis ();//12*3600000-36*3600000 minus or plus 12 hours.
Mticker = new Runnable () {
public void Run () {
The date of this reduction is 1970 time format cannot appear 00:00:00 12: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 timer thread, update regularly
Mticker.run ();
}else{
B.settext ("Start");
Stop the timer Remove any pending posts of Runnable R this 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;