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通過秒換算成時(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 + "秒";
}
通過秒分別得出多少小時(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 RecyclerView網(wǎng)格布局示例解析
這篇文章主要介紹了Android RecyclerView網(wǎng)格布局示例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Android仿知乎懸浮功能按鈕FloatingActionButton效果
前段時(shí)間在看屬性動(dòng)畫,恰巧這個(gè)按鈕的效果可以用屬性動(dòng)畫實(shí)現(xiàn),下面通過本文給大家分享adroid仿知乎懸浮功能按鈕FloatingActionButton效果,需要的朋友參考下吧2017-04-04
Android 讀取文件內(nèi)容實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實(shí)現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10
Android5.0中Material Design的新特性
這篇文章主要介紹了Android5.0中Material Design的新特性的相關(guān)資料,需要的朋友可以參考下2016-08-08
Android Handler的postDelayed()關(guān)閉的方法及遇到問題
這篇文章主要介紹了Android Handler的postDelayed()關(guān)閉的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

