1. 程式人生 > >js,java如何將秒轉換成幾時幾分幾秒

js,java如何將秒轉換成幾時幾分幾秒

 var s=parseInt(data);
                                    var format,array;
                                    var hours = Math.floor(s/(60*60));
                                    var minutes = Math.floor(s/60)-Math.floor(s/3600)*60;
                                    var second = Math.floor(s)-Math.floor(s/60)*60;
                                    if(hours>0){
//                                       minutes=minutes-hours*60;
//                                       second=second-minutes*60-hours*60*60;
                                        format="${0}時${1}分${2}秒";
                                        array=[hours,minutes,second];
                                    }else if(minutes>0){

                                        format="${0}分${1}秒";
                                        array=[minutes,second];
                                    }else{
                                        format="${0}秒";
                                        array=[second];
                                    }
                                    html= dojo.string.substitute(format,array);

本例用了dojo的API。

以下是java的轉換方法

public static String formatSecond(Object second){
		 String  html="0秒";
         if(second!=null){
             Double s=(Double) second;
             String format;
             Object[] array;
             Integer hours =(int) (s/(60*60));
             Integer minutes = (int) (s/60-hours*60);
             Integer seconds = (int) (s-minutes*60-hours*60*60);
             if(hours>0){
                 format="%1$,d時%2$,d分%3$,d秒";
                 array=new Object[]{hours,minutes,seconds};
             }else if(minutes>0){
                 format="%1$,d分%2$,d秒";
                 array=new Object[]{minutes,seconds};
             }else{
                 format="%1$,d秒";
                 array=new Object[]{seconds};
             }
             html= String.format(format, array);
         }
		
		return html;
		
	}