zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    我也要学C++_第三章:字符串
    我也要学C++_第二章:整数表达式
    VC++学习笔记01糖葫芦(数组)及其游戏编程实践
    一年后再更新博客了
    我也要学C语言(威力加强版)_第一章:WINDOWS平台下CL编译器下helloworld的编写,编译与链接
    我也要学python内置数据结构(一)
    批量转换的福音:一款很好用的文件编码批量转换工具。
    我的未来
    php解决json_encode输出GB2312中文问题 (数组)
    实现单页播放音乐的功能
  • 原文地址:https://www.cnblogs.com/javallh/p/10452726.html
Copyright © 2011-2022 走看看