Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法
在對時間進行轉(zhuǎn)換中,通常會把秒轉(zhuǎn)換成時分秒的小功能,怎么才能做到呢,其實也簡單 這就涉及到時分秒之間的相互轉(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);
}
}
這樣就把時間轉(zhuǎn)換成 00:00:00 的時間格式了
ps:下面看下android通過秒換算成時分秒
把秒換算成時分秒
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 + "時" + d + "分" + s + "秒";
}
通過秒分別得出多少小時多少分多少秒
public class TimeUtils {
public static String getHours(long second) {//計算秒有多少小時
long h = 00;
if (second > 3600) {
h = second / 3600;
}
return h+"";
}
public static String getMins(long second) {//計算秒有多少分
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) {//計算秒有多少秒
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 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法的文章就介紹到這了,更多相關(guān)Android 秒轉(zhuǎn)換成時分秒內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android編程實現(xiàn)帶有圖標的ListView并帶有長按菜單效果示例
這篇文章主要介紹了Android編程實現(xiàn)帶有圖標的ListView并帶有長按菜單效果,結(jié)合實例形式分析了Android帶圖標的ListView及菜單功能相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-06-06
Flutter onTap中讓你脫穎而出的5條規(guī)則
這篇文章主要為大家介紹了Flutter onTap中讓你脫穎而出的5條規(guī)則,小事情決定了你的熟練程度,這些小細節(jié)的有趣之處在于它們的豐富性2023-11-11
Android手機開發(fā) 使用線性布局和相對布局實現(xiàn)Button垂直水平居中
本文主要結(jié)合自己的理解分別對使用LinearLayout和RelativeLayout兩種方式實現(xiàn)居中做了總結(jié),希望對大家有所幫助。2016-05-05
Native.js獲取監(jiān)聽開關(guān)等操作Android藍牙設(shè)備實例代碼
本文為大家分享了Native.js對Android藍牙設(shè)備的操作實例代碼包括:監(jiān)聽藍牙開關(guān)狀態(tài),開啟關(guān)閉藍牙,獲取藍牙設(shè)備列表,藍牙連接票據(jù)打印機2018-09-09
Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實現(xiàn)
這篇文章主要介紹了Android開發(fā)之a(chǎn)ndroid_gps定位服務(wù)簡單實現(xiàn) ,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
Android 解決嵌套Fragment無法接收onCreateOptionsMenu事件的問題
本文主要介紹Android Fragment無法接收onCreateOptionsMenu事件的問題,這里給出解決辦法以及詳細代碼,希望能幫助有需要的小伙伴2016-07-07

