Android视频播放项目总结之 把毫秒转化成时、分、秒的工具类(自己分装的)
2015-05-01 08:31
591 查看
public class Utils {
private StringBuilder mFormatBuilder;
private Formatter mFormatter;
public Utils() {
// 转换成字符串的时间
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
}
/**
* 把毫秒转换成:1:20:30这里形式
* @param timeMs
* @return
*/
public String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
}
工具类调用方法:
Utils utils = new Utils();
String duration =utils.stringForTime(传入int类型的毫秒);
duration 就是时分秒格式的时间。
private StringBuilder mFormatBuilder;
private Formatter mFormatter;
public Utils() {
// 转换成字符串的时间
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
}
/**
* 把毫秒转换成:1:20:30这里形式
* @param timeMs
* @return
*/
public String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
}
工具类调用方法:
Utils utils = new Utils();
String duration =utils.stringForTime(传入int类型的毫秒);
duration 就是时分秒格式的时间。
相关文章推荐
- Android视频播放项目总结之 使用VideoView定义自己的视频播放器
- Android视频播放项目总结之 使用Android中的videoView自己定义,暂停,播放时间,总时间,进度等。
- Android视频播放项目总结之 思路梳理
- Android视频播放项目总结之 得到手机中的所有视频数据
- Android视频播放项目总结之 适配器绑定视频信息列表显示到界面(适配器简单优化)
- Android视频播放项目总结之 点击listview条目调用手机上其他的视频播放器播放
- Android视频播放项目总结之 让别的程序能调用我的视频播放器
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(一)
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(二)
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(三)
- Android视频播放项目总结之 使用第三方Vitamio库,开发万能播放器(四)
- Android视频播放项目总结之 代码分享
- Android项目R文件丢失问题—自己总结
- 【转】Android WebView 播放视频总结
- 使用Vitamio打造自己的Android万能播放器(12)—— 播放网络视频缓冲处理
- Android电话短信拦截项目总结之 开发工具
- 使用Vitamio开源项目在Android实现播放网络视频
- 工作中的一个视频播放项目总结及备份
- 王立平--android项目的开发设计思路,自己总结的,仅供参考
- Android中分装Log,指定自己的日志工具