摄像头回放
This commit is contained in:
@ -0,0 +1,146 @@
|
||||
package org.dromara.common.core.utils;
|
||||
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 时间戳转换工具类(包含时分秒提取)
|
||||
*/
|
||||
public class TimestampUtils {
|
||||
|
||||
// 默认时区(东八区)
|
||||
private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("GMT+8");
|
||||
|
||||
// 完整日期格式
|
||||
private static final String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
// 时分秒格式
|
||||
private static final String TIME_FORMAT = "HH:mm:ss";
|
||||
|
||||
// 日期格式
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
/**
|
||||
* 将日期字符串转为秒级时间戳(优化版)
|
||||
* 支持格式:"2025-12-03 23:59:59"、"2025-12-03"等
|
||||
* @param dateStr 日期字符串
|
||||
* @return 秒级时间戳
|
||||
*/
|
||||
public static Long parseDateToTimestamp(String dateStr) {
|
||||
if (StrUtil.isBlank(dateStr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 使用Hutool的DateUtil进行智能解析(推荐)
|
||||
try {
|
||||
Date date = DateUtil.parse(dateStr);
|
||||
return date.getTime() / 1000L; // 转为秒级时间戳
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("日期格式错误:%s,支持格式:yyyy-MM-dd HH:mm:ss、yyyy-MM-dd等", dateStr)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将时间戳转换为完整日期格式(yyyy-MM-dd HH:mm:ss)
|
||||
*/
|
||||
public static String formatTimestamp(Object timestamp) {
|
||||
if (timestamp == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String timestampStr = timestamp.toString().trim();
|
||||
if (StrUtil.isBlank(timestampStr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Long time = parseToMilliseconds(timestampStr);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(FULL_DATE_FORMAT);
|
||||
sdf.setTimeZone(DEFAULT_TIME_ZONE);
|
||||
return sdf.format(new Date(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取时间戳中的时分秒部分(HH:mm:ss)
|
||||
*/
|
||||
public static String extractTime(Object timestamp) {
|
||||
if (timestamp == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String timestampStr = timestamp.toString().trim();
|
||||
if (StrUtil.isBlank(timestampStr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Long time = parseToMilliseconds(timestampStr);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
|
||||
sdf.setTimeZone(DEFAULT_TIME_ZONE);
|
||||
return sdf.format(new Date(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取时间戳中的日期部分(yyyy-MM-dd)
|
||||
*/
|
||||
public static String extractDate(Object timestamp) {
|
||||
if (timestamp == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String timestampStr = timestamp.toString().trim();
|
||||
if (StrUtil.isBlank(timestampStr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Long time = parseToMilliseconds(timestampStr);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
|
||||
sdf.setTimeZone(DEFAULT_TIME_ZONE);
|
||||
return sdf.format(new Date(time));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析时间戳字符串为毫秒级时间戳
|
||||
*/
|
||||
private static Long parseToMilliseconds(String timestampStr) {
|
||||
try {
|
||||
Long time = Long.parseLong(timestampStr);
|
||||
// 10位秒级时间戳转为13位毫秒级
|
||||
if (timestampStr.length() == 10) {
|
||||
time = time * 1000;
|
||||
}
|
||||
return time;
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("时间戳格式错误:" + timestampStr);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间戳对应的小时
|
||||
*/
|
||||
public static int getHour(Object timestamp) {
|
||||
String timeStr = extractTime(timestamp);
|
||||
return Integer.parseInt(timeStr.split(":")[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间戳对应的分钟
|
||||
*/
|
||||
public static int getMinute(Object timestamp) {
|
||||
String timeStr = extractTime(timestamp);
|
||||
return Integer.parseInt(timeStr.split(":")[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取时间戳对应的秒
|
||||
*/
|
||||
public static int getSecond(Object timestamp) {
|
||||
String timeStr = extractTime(timestamp);
|
||||
return Integer.parseInt(timeStr.split(":")[2]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user