1.计算文件大小,并按**B,*KB,**MB,**GB格式返回字符串
/** * 文件大小格式转换 * @param fileS 文件大小 * @return 文件大小 */ public static String FormetFileSize(long fileS) { // 转换文件大小 DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "KB"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "GB"; } return fileSizeString; }
2.毫秒转换为h:mm:ss字符串格式方法
1 private String stringForTime(int timeMs) { 2 int totalSeconds = timeMs / 1000; 3 int seconds = totalSeconds % 60; 4 int minutes = (totalSeconds / 60) % 60; 5 int hours = totalSeconds / 3600; 6 if (hours > 0) { 7 return String.format("%d:%02d:%02d", hours, minutes, seconds); 8 } else { 9 return String.format("%02d:%02d", minutes, seconds); 10 } 11 }
