public class TimeStringFormat { /** * * @Title: stringToTime1 * @Description: 给定规定格式的字符串转换成秒 * @return long 返回类型 * @throws */ public static long stringToTime1(String str) { String[] strArr = str.split(":"); int result = 0; int i = 3600; for(String st : strArr) { result += Integer.parseInt(st) * i; i = i/60; } return Long.parseLong(result+""); } /** * * @Title: stringToTime2 * @Description: 给定规定格式的字符串转换成秒 * @return long 返回类型 * @throws */ public static long stringToTime2(String str) { int hour,min,second; hour = Integer.parseInt(str.substring(0, str.indexOf(":"))); min = Integer.parseInt(str.substring(str.indexOf(":") + 1, str.lastIndexOf(":"))); second = Integer.parseInt(str.substring(str.lastIndexOf(":") + 1)); return Long.parseLong(hour*3600 + min*60 + second + ""); } /** * * @Title: timeToString * @Description: 给定秒 转换成指定的字符格式 * @return String 返回类型 * @throws */ public static String timeToString(Long time) { int hour,min,second; String hourStr,minStr,secondStr; hourStr = minStr = secondStr = ""; hour = Integer.parseInt(time/3600 + ""); min = Integer.parseInt((time - hour*3600)/60 + ""); second = Integer.parseInt(time - hour*3600 - min*60 +""); hourStr = hour < 10 ? "0" + hour : hour + ""; minStr = min < 10 ? "0" + min : min + ""; secondStr = second < 10 ? "0" + second : second + ""; return hourStr + ":" + minStr + ":" + secondStr; } /** * * @Title: * @Description: 测试方法 * @return void 返回类型 * @throws */ public static void main(String[] args) { System.out.println(stringToTime1("05:06:23")); System.out.println(stringToTime2("05:06:23")); System.out.println(timeToString(new Long(18383))); } }