这篇文章主要介绍了Android 实现秒转换成时分秒的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
在对时间进行转换中,通常会把秒转换成时分秒的小功能,怎么才能做到呢,其实也简单 这就涉及到时分秒之间的相互转换
具体代码如下:
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);
}
}
这样就把时间转换成 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 + "";
}
}
总结
到此这篇关于Android 实现秒转换成时分秒的方法的文章就介绍到这了,更多相关Android 秒转换成时分秒内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Android 实现秒转换成时分秒的方法
猜你喜欢
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Flutter实现底部和顶部导航栏 2022-08-31
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- Android实现轮询的三种方式 2023-02-17
- Android studio实现动态背景页面 2023-05-23
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android实现监听音量的变化 2023-03-30
