把秒转换为时分秒

来源:互联网 发布:win7网络位置更改不了 编辑:程序博客网 时间:2024/05/21 09:53
[java] view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) throws Exception {  
  3.         System.out.println(secondsToTime(14555));  
  4.     }  
  5.     /*** 
  6.      * 把秒转换为时分秒 
  7.      * @param seconds 
  8.      * @return 
  9.      */  
  10.     public static String secondsToTime(long seconds) {  
  11.         long h = 0;  
  12.         long d = 0;  
  13.         long s = 0;  
  14.         long temp = seconds % 3600;  
  15.         if (seconds > 3600) {  
  16.             h = seconds / 3600;  
  17.             if (temp != 0) {  
  18.                 if (temp > 60) {  
  19.                     d = temp / 60;  
  20.                     if (temp % 60 != 0) {  
  21.                         s = temp % 60;  
  22.                     }  
  23.                 } else {  
  24.                     s = temp;  
  25.                 }  
  26.             }  
  27.         } else {  
  28.             d = seconds / 60;  
  29.             if (seconds % 60 != 0) {  
  30.                 s = seconds % 60;  
  31.             }  
  32.         }  
  33.         return (h < 10 ? "0" + h : h) + ":" + (d < 10 ? "0" + d : d) + ":"  
  34.                 + (s < 10 ? "0" + s : s);  
  35.     }  
  36. }  
原创粉丝点击