栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

文章目录
  • Java Date 时间的转换、以及获取直播时长
    • 1.DateUtil
  • 总结


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 {

    
    public static String convertData(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ds=df.format(date);
        return ds;
    }

    
    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()
            return true;
        }else{
            return false;
        }
    }

	
	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;
	}


	
	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;
	}
}

总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号