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

时间:2023-03-09 04:02:56
java将秒转换为时分秒工具类

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

 package com.dq.schoolcontract.utils;

 import com.sun.media.jfxmedia.control.VideoRenderControl;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*; /**
* @Author Allen.Lv
* @Description //TODO
* @Date 16:43 2019/2/27
* @Desc: Coding Happy!
**/
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "视频时长")
public class VideoDuration { /**
* 视频时长秒
*/
@ApiModelProperty(value = "秒")
private Integer second; /**
* 视频时长分
*/
@ApiModelProperty("分")
private Integer minute; /**
* 视频时长时
*/
@ApiModelProperty(value = "时")
private Integer hour; }

下面为转换工具类:

 package com.dq.schooldomain.utils;

 import com.dq.schoolcontract.utils.VideoDuration;

 /**
* @Author Allen.Lv
* @Description //TODO
* @Date 19:43 2019/2/28
* @Desc: Coding Happy!
**/
public class SecToTime { public static VideoDuration secToTime(int time) {
String timeStr = null;
int hour = 0;
int minute = 0;
int second = 0;
if (time <= 0)
return new VideoDuration(0, 0, 0);
else {
minute = time / 60;
if (minute < 60) {
second = time % 60;
timeStr = unitFormat(minute) + ":" + unitFormat(second);
} else {
hour = minute / 60;
if (hour > 99)
return new VideoDuration(59, 59, 99);
minute = minute % 60;
second = time - hour * 3600 - minute * 60;
timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
}
}
return new VideoDuration(Integer.parseInt(unitFormat(second)), Integer.parseInt(unitFormat(minute)), Integer.parseInt(unitFormat(hour)));
} private static String unitFormat(int i) {
String retStr = null;
if (i >= 0 && i < 10)
retStr = "0" + i;
else
retStr = "" + i;
return retStr;
}
}