Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法
在對(duì)時(shí)間進(jìn)行轉(zhuǎn)換中,通常會(huì)把秒轉(zhuǎn)換成時(shí)分秒的小功能,怎么才能做到呢,其實(shí)也簡(jiǎn)單 這就涉及到時(shí)分秒之間的相互轉(zhuǎn)換
具體代碼如下:
import android.content.Context; public class ToolsUtil { private static ToolsUtil toolsUtil; private Context mContext; private ToolsUtil(Context context) { mContext = context.getApplicationContext(); } public static ToolsUtil getInstance(Context context) { if (toolsUtil == null) { toolsUtil = new ToolsUtil(context); } return toolsUtil; } public String timeConversion(int time) { int hour = 0; int minutes = 0; int sencond = 0; int temp = time % 3600; if (time > 3600) { hour = time / 3600; if (temp != 0) { if (temp > 60) { minutes = temp / 60; if (temp % 60 != 0) { sencond = temp % 60; } } else { sencond = temp; } } } else { minutes = time / 60; if (time % 60 != 0) { sencond = time % 60; } } return (hour<10?("0"+hour):hour) + ":" + (minutes<10?("0"+minutes):minutes) + ":" + (sencond<10?("0"+sencond):sencond); } }
這樣就把時(shí)間轉(zhuǎn)換成 00:00:00 的時(shí)間格式了
ps:下面看下android通過(guò)秒換算成時(shí)分秒
把秒換算成時(shí)分秒
public static String cal(int second) { int h = 0; int d = 0; int s = 0; int temp = second % 3600; if (second > 3600) { h = second / 3600; if (temp != 0) { if (temp > 60) { d = temp / 60; if (temp % 60 != 0) { s = temp % 60; } } else { s = temp; } } } else { d = second / 60; if (second % 60 != 0) { s = second % 60; } } return h + "時(shí)" + d + "分" + s + "秒"; }
通過(guò)秒分別得出多少小時(shí)多少分多少秒
public class TimeUtils { public static String getHours(long second) {//計(jì)算秒有多少小時(shí) long h = 00; if (second > 3600) { h = second / 3600; } return h+""; } public static String getMins(long second) {//計(jì)算秒有多少分 long d = 00; long temp = second % 3600; if (second > 3600) { if (temp != 0) { if (temp > 60) { d = temp / 60; } } } else { d = second / 60; } return d + ""; } public static String getSeconds(long second) {//計(jì)算秒有多少秒 long s = 0; long temp = second % 3600; if (second > 3600) { if (temp != 0) { if (temp > 60) { if (temp % 60 != 0) { s = temp % 60; } } else { s = temp; } } } else { if (second % 60 != 0) { s = second % 60; } } return s + ""; } }
總結(jié)
到此這篇關(guān)于Android 實(shí)現(xiàn)秒轉(zhuǎn)換成時(shí)分秒的方法的文章就介紹到這了,更多相關(guān)Android 秒轉(zhuǎn)換成時(shí)分秒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長(zhǎng)按菜單效果示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長(zhǎng)按菜單效果,結(jié)合實(shí)例形式分析了Android帶圖標(biāo)的ListView及菜單功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢的相關(guān)資料,需要的朋友可以參考下2016-04-04Flutter onTap中讓你脫穎而出的5條規(guī)則
這篇文章主要為大家介紹了Flutter onTap中讓你脫穎而出的5條規(guī)則,小事情決定了你的熟練程度,這些小細(xì)節(jié)的有趣之處在于它們的豐富性2023-11-11Android手機(jī)開(kāi)發(fā) 使用線性布局和相對(duì)布局實(shí)現(xiàn)Button垂直水平居中
本文主要結(jié)合自己的理解分別對(duì)使用LinearLayout和RelativeLayout兩種方式實(shí)現(xiàn)居中做了總結(jié),希望對(duì)大家有所幫助。2016-05-05Native.js獲取監(jiān)聽(tīng)開(kāi)關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
本文為大家分享了Native.js對(duì)Android藍(lán)牙設(shè)備的操作實(shí)例代碼包括:監(jiān)聽(tīng)藍(lán)牙開(kāi)關(guān)狀態(tài),開(kāi)啟關(guān)閉藍(lán)牙,獲取藍(lán)牙設(shè)備列表,藍(lán)牙連接票據(jù)打印機(jī)2018-09-09Android開(kāi)發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Android開(kāi)發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡(jiǎn)單實(shí)現(xiàn) ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Flutter開(kāi)發(fā)中的路由參數(shù)處理
在實(shí)際開(kāi)發(fā)中,我們經(jīng)常會(huì)需要在頁(yè)面跳轉(zhuǎn)的時(shí)候攜帶路由參數(shù),典型的例子就是從列表到詳情頁(yè)的時(shí)候,需要攜帶詳情的 id,以便詳情頁(yè)獲取對(duì)應(yīng)的數(shù)據(jù)。同時(shí),有些時(shí)候還需要返回時(shí)攜帶參數(shù)返回上一級(jí),以便上級(jí)頁(yè)面根據(jù)返回結(jié)果更新。本篇將介紹這兩種情形的實(shí)現(xiàn)。2021-06-06Android 解決嵌套Fragment無(wú)法接收onCreateOptionsMenu事件的問(wèn)題
本文主要介紹Android Fragment無(wú)法接收onCreateOptionsMenu事件的問(wèn)題,這里給出解決辦法以及詳細(xì)代碼,希望能幫助有需要的小伙伴2016-07-07android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09