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;
}
}
总结
一些工作中方便自己重新使用的小方法、后续会不断加深。