赞助

java将秒转换为时分秒工具类

需要一个接收时分秒的对象,如下:

 1 package com.dq.schoolcontract.utils;
 2 
 3 import com.sun.media.jfxmedia.control.VideoRenderControl;
 4 import io.swagger.annotations.ApiModel;
 5 import io.swagger.annotations.ApiModelProperty;
 6 import lombok.*;
 7 
 8 /**
 9  * @Author Allen.Lv
10  * @Description //TODO
11  * @Date 16:43 2019/2/27
12  * @Desc: Coding Happy!
13  **/
14 @Getter
15 @Setter
16 @ToString
17 @AllArgsConstructor
18 @NoArgsConstructor
19 @ApiModel(value = "视频时长")
20 public class VideoDuration {
21 
22     /**
23      * 视频时长秒
24      */
25     @ApiModelProperty(value = "秒")
26     private Integer second;
27 
28     /**
29      * 视频时长分
30      */
31     @ApiModelProperty("分")
32     private Integer minute;
33 
34     /**
35      * 视频时长时
36      */
37     @ApiModelProperty(value = "时")
38     private Integer hour;
39 
40 
41 }

下面为转换工具类:

 1 package com.dq.schooldomain.utils;
 2 
 3 import com.dq.schoolcontract.utils.VideoDuration;
 4 
 5 /**
 6  * @Author Allen.Lv
 7  * @Description //TODO
 8  * @Date 19:43 2019/2/28
 9  * @Desc: Coding Happy!
10  **/
11 public class SecToTime {
12 
13     public static VideoDuration secToTime(int time) {
14         String timeStr = null;
15         int hour = 0;
16         int minute = 0;
17         int second = 0;
18         if (time <= 0)
19             return new VideoDuration(0, 0, 0);
20         else {
21             minute = time / 60;
22             if (minute < 60) {
23                 second = time % 60;
24                 timeStr = unitFormat(minute) + ":" + unitFormat(second);
25             } else {
26                 hour = minute / 60;
27                 if (hour > 99)
28                     return new VideoDuration(59, 59, 99);
29                 minute = minute % 60;
30                 second = time - hour * 3600 - minute * 60;
31                 timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
32             }
33         }
34         return new VideoDuration(Integer.parseInt(unitFormat(second)), Integer.parseInt(unitFormat(minute)), Integer.parseInt(unitFormat(hour)));
35     }
36 
37     private static String unitFormat(int i) {
38         String retStr = null;
39         if (i >= 0 && i < 10)
40             retStr = "0" + i;
41         else
42             retStr = "" + i;
43         return retStr;
44     }
45 }

 

posted @ 2019-02-28 19:48  念念不忘、  阅读(15743)  评论(0编辑  收藏  举报