Android 实现秒转换成时分秒的方法
在对时间进行转换中,通常会把秒转换成时分秒的小功能,怎么才能做到呢,其实也简单这就涉及到时分秒之间的相互转换
具体代码如下:
importandroid.content.Context; publicclassToolsUtil{ privatestaticToolsUtiltoolsUtil; privateContextmContext; privateToolsUtil(Contextcontext){ mContext=context.getApplicationContext(); } publicstaticToolsUtilgetInstance(Contextcontext){ if(toolsUtil==null){ toolsUtil=newToolsUtil(context); } returntoolsUtil; } publicStringtimeConversion(inttime){ inthour=0; intminutes=0; intsencond=0; inttemp=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通过秒换算成时分秒
把秒换算成时分秒
publicstaticStringcal(intsecond){ inth=0; intd=0; ints=0; inttemp=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; } } returnh+"时"+d+"分"+s+"秒"; }
通过秒分别得出多少小时多少分多少秒
publicclassTimeUtils{ publicstaticStringgetHours(longsecond){//计算秒有多少小时 longh=00; if(second>3600){ h=second/3600; } returnh+""; } publicstaticStringgetMins(longsecond){//计算秒有多少分 longd=00; longtemp=second%3600; if(second>3600){ if(temp!=0){ if(temp>60){ d=temp/60; } } }else{ d=second/60; } returnd+""; } publicstaticStringgetSeconds(longsecond){//计算秒有多少秒 longs=0; longtemp=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; } } returns+""; } }
总结
到此这篇关于Android实现秒转换成时分秒的方法的文章就介绍到这了,更多相关Android秒转换成时分秒内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!