日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

完整的Android程序,主要功能是實(shí)現(xiàn)秒表計(jì)時(shí)

需求:
默認(rèn)為"00:00:00",點(diǎn)擊開(kāi)始按鈕時(shí)清零后開(kāi)始計(jì)時(shí),出現(xiàn)如10:28:34。點(diǎn)擊停止的時(shí)候停止計(jì)時(shí)。

問(wèn)題:
使用Calendar DateFormat的方法,不設(shè)置時(shí)區(qū)獲取到的小時(shí)是本地時(shí)區(qū)的(東八區(qū)的就是8),設(shè)置成GMT標(biāo)準(zhǔn)時(shí)區(qū)獲取到的時(shí)間是12小時(shí)(12:00:00),設(shè)置24小時(shí)制無(wú)效。

在開(kāi)始時(shí)間加減各種小時(shí)都無(wú)效,而且計(jì)時(shí)只能到12小時(shí)就自動(dòng)跳上去了,始終無(wú)法出現(xiàn)默認(rèn)狀態(tài)00:00:00開(kāi)始計(jì)時(shí)的效果。
嘗試各種時(shí)間設(shè)置方法無(wú)效后只能自己寫一個(gè)根據(jù)秒數(shù)轉(zhuǎn)換時(shí)間格式字符串的方法了,經(jīng)過(guò)測(cè)試是沒(méi)問(wèn)題的,兩位數(shù)只能顯示99小時(shí)為最大,如需要更大小時(shí)數(shù)需要改改方法。
另外小時(shí)數(shù)也不能無(wú)限大,超過(guò)long數(shù)據(jù)類型長(zhǎng)度會(huì)變成負(fù)數(shù)的,會(huì)出現(xiàn)異常的。

顯示效果:

測(cè)試類:

 1 public class TestTime {

 2     public static void main(String[] args) {
 3         TestTime tt = new TestTime();
 4         tt.showTimeCount(99*3600000+75*1000);
 5     }
 6     
 7     //時(shí)間計(jì)數(shù)器,最多只能到99小時(shí),如需要更大小時(shí)數(shù)需要改改方法
 8     public String showTimeCount(long time) {
 9         System.out.println("time="+time);
10         if(time >= 360000000){
11             return "00:00:00";
12         }
13         String timeCount = "";
14         long hourc = time/3600000;
15         String hour = "0" + hourc;
16         System.out.println("hour="+hour);
17         hour = hour.substring(hour.length()-2, hour.length());
18         System.out.println("hour2="+hour);
19         
20         long minuec = (time-hourc*3600000)/(60000);
21         String minue = "0" + minuec;
22         System.out.println("minue="+minue);
23         minue = minue.substring(minue.length()-2, minue.length());
24         System.out.println("minue2="+minue);
25         
26         long secc = (time-hourc*3600000-minuec*60000)/1000;
27         String sec = "0" + secc;
28         System.out.println("sec="+sec);
29         sec = sec.substring(sec.length()-2, sec.length());
30         System.out.println("sec2="+sec);
31         timeCount = hour + ":" + minue + ":" + sec;
32         System.out.println("timeCount="+timeCount);
33         return timeCount;
34     }
35     
36 }


實(shí)際例子:

 1 //時(shí)間計(jì)數(shù)器,最多只能到99小時(shí),如需要更大小時(shí)數(shù)需要改改方法

 2     public String showTimeCount(long time) {
 3         if(time >= 360000000){
 4             return "00:00:00";
 5         }
 6         String timeCount = "";
 7         long hourc = time/3600000;
 8         String hour = "0" + hourc;
 9         hour = hour.substring(hour.length()-2, hour.length());
10         
11         long minuec = (time-hourc*3600000)/(60000);
12         String minue = "0" + minuec;
13         minue = minue.substring(minue.length()-2, minue.length());
14         
15         long secc = (time-hourc*3600000-minuec*60000)/1000;
16         String sec = "0" + secc;
17         sec = sec.substring(sec.length()-2, sec.length());
18         timeCount = hour + ":" + minue + ":" + sec;
19         return timeCount;
20     }
21     
22     private Handler stepTimeHandler;
23     private Runnable mTicker;
24     long startTime = 0;
25     
26     //開(kāi)始按鈕
27     class startBtnListener implements OnClickListener {
28         @Override
29         public void onClick(View v) {
30             Button b = (Button)v;
31             String buttonText = b.getText().toString();
32             if("Start".equalsIgnoreCase(buttonText)){
33                 b.setText("Stop");
34                 // 清零 開(kāi)始計(jì)時(shí)
35                 stepTimeTV.setText("00:00:00");
36                 stepTimeHandler = new Handler();
37                 startTime = System.currentTimeMillis();
38                 mTicker = new Runnable() {
39                     public void run() {
40                         String content = showTimeCount(System.currentTimeMillis() - startTime);
41                         stepTimeTV.setText(content);
42 
43                         long now = SystemClock.uptimeMillis();
44                         long next = now + (1000 - now % 1000);
45                         stepTimeHandler.postAtTime(mTicker, next);
46                     }
47                 };
48                 //啟動(dòng)計(jì)時(shí)線程,定時(shí)更新
49                 mTicker.run();
50             }else{
51                 b.setText("Start");
52                 //停止計(jì)時(shí) Remove any pending posts of Runnable r that are in the message queue.
53                 stepTimeHandler.removeCallbacks(mTicker);
54             }
55         }
56     }


用時(shí)間格式化的方式測(cè)試代碼:

 1     //開(kāi)始按鈕 通過(guò)Calendar時(shí)間設(shè)置的方式,無(wú)法正常顯示小時(shí)為0

 2     class startBtnListener implements OnClickListener {
 3         @Override
 4         public void onClick(View v) {
 5             Button b = (Button)v;
 6             String buttonText = b.getText().toString();
 7             if("Start".equalsIgnoreCase(buttonText)){
 8                 b.setText("Stop");
 9                 // 清零 開(kāi)始計(jì)時(shí)
10                 stepTimeTV.setText("00:00:00");
11                 if (mCalendar == null) {
12                     mCalendar = Calendar.getInstance();
13                     TimeZone tz = TimeZone.getTimeZone("GMT");//GMT+8
14                     mCalendar.setTimeZone(tz);
15                     mCalendar.get(Calendar.HOUR_OF_DAY);//24小時(shí)制
16                 }
17                 stepTimeHandler = new Handler();
18                 //System.uptimeMillis()         //記錄從機(jī)器啟動(dòng)后到現(xiàn)在的毫秒數(shù),當(dāng)系統(tǒng)進(jìn)入深度睡眠時(shí),此計(jì)時(shí)器將會(huì)停止
19                 //System.currentTimeMillis()   //返回自1970年1月1日到現(xiàn)在的毫秒數(shù),通常用來(lái)設(shè)置日期和時(shí)間
20                 //System.elapsedRealtime()   //返回從機(jī)器啟動(dòng)后到現(xiàn)在的毫秒數(shù),包括系統(tǒng)深度睡眠的時(shí)間,api里沒(méi)有這個(gè)方法
21                 //直接取得的是當(dāng)?shù)貢r(shí)區(qū)時(shí)間,當(dāng)?shù)貢r(shí)間跟時(shí)區(qū)有關(guān),設(shè)置GMT后始終多12小時(shí)
22                 startTime = System.currentTimeMillis();//12*3600000  - 36*3600000減掉或者加上12小時(shí)都不行 
3                 mTicker = new Runnable() {
24                     public void run() {
25                         //這個(gè)減出來(lái)的日期是1970年的  時(shí)間格式不能出現(xiàn)00:00:00 12:00:00
26                         long showTime = System.currentTimeMillis() - startTime;
27                         Log.i(TAG,showTime+"");
28                         mCalendar.setTimeInMillis(showTime + 13*3600000 + 1000);
29                         String content = (String) DateFormat.format(mFormat, mCalendar);
30                         stepTimeTV.setText(content);
31 
32                         long now = SystemClock.uptimeMillis();
33                         long next = now + (1000 - now % 1000);
34                         stepTimeHandler.postAtTime(mTicker, next);
35                     }
36                 };
37                 //啟動(dòng)計(jì)時(shí)線程,定時(shí)更新
38                 mTicker.run();
39             }else{
40                 b.setText("Start");
41                 //停止計(jì)時(shí) Remove any pending posts of Runnable r that are in the message queue.
42                 stepTimeHandler.removeCallbacks(mTicker);
43             }
44         }
45     }
46     
47     private Handler stepTimeHandler;
48     Calendar mCalendar;
49     String mFormat = "yyyy-MM-dd hh:mm:ss";//yyyy-MM-dd
50     long startTime = 0;
51     private Runnable mTicker;


分享到:
標(biāo)簽:計(jì)時(shí)器 格式化 計(jì)數(shù)器 時(shí)間 android
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定