其中要用的工具包:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.1</version>
</dependency>
/**
* 将秒数转成hh:mm:ss的时间
* @param time
* @return
*/
public static String TimeToString(long time){
SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("GTM+0"));
return sf.format(time*1000);
}
/**
* 将格式为HH:mm:ss的时间转成时间戳(从00:00:00开始计算)
* @param time
* @return
* @throws Exception
*/
public static int StringToTime(String time) throws Exception {
SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("GTM+0"));
Date parse2 = sf.parse(time);
return Convert.toInt((parse2.getTime()) / 1000);
}
/**
* 将格式为 HH:mm:ss - HH:mm:ss的字符串转成一个时间戳的数组
* @param srt
* @return
* @throws Exception
*/
public static Integer[] toStringToTime(Object srt) throws Exception {
String s1 = srt.toString();
String[] s = s1.split("-");
Integer[] l = new Integer[2];
l[0] = StringToTime(s[0]);
l[1] = StringToTime(s[1]);
return l;
}
/**
* 将前端传来的时间字符转化成两个分为开始时间和结束时间
* @param str 格式 yyyy-MM-dd - yyyy-MM-dd
* @return
*/
public static Long[] SplitDate(Object str) {
String s = str.toString();
String[] split = s.split(" - ");
long begin = StringToTimestamp(split[0], "yyyy-MM-dd HH:mm:ss");
long end = StringToTimestamp(split[1], "yyyy-MM-dd HH:mm:ss");
Long[] arra = {begin, end};
return arra;
}
/**
* 将时间形式的字符串转成时间戳
*yyyy-MM-dd
* @return
*/
public static long StringToTimestamp(String time, String format) {
LocalDateTime parse1 = LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format));
ZoneId zone = ZoneId.systemDefault();
Instant instant = parse1.atZone(zone).toInstant();
Long l = instant.toEpochMilli();
String s = l.toString();
String sub = StrUtil.sub(s, 0, -3);
return Long.parseLong(sub);
}
还没有评论,来说两句吧...