Java Date 时间的转换、以及获取直播时长

Java Date 时间的转换、以及获取直播时长


1.DateUtil

package com.qc.zh.util;

import com.qc.zh.enumerate.StatusEnum;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DateUtil {

    /**
     *  Data 转 String
     * @param date  时间
     * @return
     */
    public static String convertData(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ds=df.format(date);
        return ds;
    }

    /**
     *  判断当前时间与直播视频播放时间大小
     * @param startTime 开始时间
     * @param stopTime  结束时间
     * @return
     */
    public static Integer compare(Date startTime, Date stopTime){
        // 返回正值是代表左侧日期大于参数日期,反之亦然,日期格式必须一致
        Integer status = null;
        String date = convertData(new Date());
        String start = convertData(startTime);
        String stop = convertData(stopTime);

        int i = date.compareTo(start);
        int i1 = date.compareTo(stop);
        if (i < 0) {    // 当前时间小于开始时间
            status = StatusEnum.DNS.getCode();
        } else if (i > 0 && i1 < 0){     // 当前时间大于开始时间 且 小于结束时间
            status = StatusEnum.START.getCode();
        } else if (i1 > 0){     // 当前时间大于结束时间
            status = StatusEnum.END.getCode();
        }
        return status;
    }

    //根据秒获取时间格式   结果为 :  long i =  2591881;   29天23小时58分钟1秒
    public static String formatDateTime(long mss) {
        String DateTimes = null;
        long days = mss / ( 60 * 60 * 24);
        long hours = (mss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (mss % ( 60 * 60)) /60;
        long seconds = mss % 60;
        if(days>0){
            DateTimes= days + "天" + hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        }else if(hours>0){
            DateTimes=hours + "小时" + minutes + "分钟"
                    + seconds + "秒";
        }else if(minutes>0){
            DateTimes=minutes + "分钟"
                    + seconds + "秒";
        }else{
            DateTimes=seconds + "秒";
        }

        return DateTimes;
    }

    public static boolean judgeTheSizeOfTime(Date dateTime) throws ParseException {
        Date date=new Date();
        if(date.getTime()<dateTime.getTime()){
            return true;
        }else{
            return false;
        }
    }

	/**
	 * 获取视频时长 秒
	 * @param fileUrl
	 * @return
	 */
	public static Integer ReadVideoTime(String fileUrl) throws Exception{
	//读取url http://a/a.mp4
		URL URL = new URL(fileUrl);
		//读取本地地址 d:test/test.mp4
      //  File source=new File(FileUrl);
		Integer length = 0;
		try {
			MultimediaObject instance = new MultimediaObject(URL);
			MultimediaInfo result = instance.getInfo();
			long ls = result.getDuration() / 1000;
		//时
		//	Integer hour = (int) (ls / 3600);
		//分
		//	Integer minute = (int) (ls % 3600) / 60;
		//秒
 		//	Integer second = (int) (ls - hour * 3600 - minute * 60);
		length = (int)ls;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return length;
	}


	/**
	 * 获取视频时长 分
	 * @param fileUrl
	 * @return
	 */
	public static Integer MinuteVideoTime(String fileUrl) throws Exception{
		//读取url http://a/a.mp4
		URL URL = new URL(fileUrl);
		//读取本地地址 d:test/test.mp4
		//  File source=new File(FileUrl);
		Integer length =0;
		try {
			MultimediaObject instance = new MultimediaObject(URL);
			MultimediaInfo result = instance.getInfo();
			long ls = result.getDuration() / 1000;
			//时
			//	Integer hour = (int) (ls / 3600);
			//分
				Integer minute = (int) (ls % 3600) / 60;
			//秒
			//	Integer second = (int) (ls - hour * 3600 - minute * 60);
			length =(int)minute;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return length;
	}
}

总结

一些工作中方便自己重新使用的小方法、后续会不断加深。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值