目录
1.1 根据传入的两个localDateTime计算时间差并转换为 对应的几天几时几分几秒
1.2 根据传入的两个localDateTime计算时间差并转换为 对应的 00:00:00 格式的 时分秒
1.3 根据传入的两个localDateTime计算时间差并转换为 对应的 00:00 格式的分秒
1.4 根据传入时分秒(分秒)格式00:00:00(00:00)转换为秒数
时间转换工具类(在示例代码后),复制即用
1. API示例:
1.1 根据传入的两个localDateTime计算时间差并转换为 对应的几天几时几分几秒
LocalDateTime start = LocalDateTime.of(2022, 2, 21, 7, 20, 30);
LocalDateTime end = LocalDateTime.of(2022, 2, 22, 8, 30, 50);
String date = GetDateUtils.getDate(start, end);
System.out.println(date); // 输出 1天1时10分20秒
1.2 根据传入的两个localDateTime计算时间差并转换为 对应的 00:00:00 格式的 时分秒
LocalDateTime start = LocalDateTime.of(2022, 2, 21, 7, 20, 30);
LocalDateTime end = LocalDateTime.of(2022, 2, 22, 8, 30, 50);
String date = GetDateUtils.getHMS(start, end);
System.out.println(date); // 输出 25:10:20 即相差25时10分钟20秒
1.3 根据传入的两个localDateTime计算时间差并转换为 对应的 00:00 格式的分秒
LocalDateTime start = LocalDateTime.of(2022, 2, 21, 7, 20, 30);
LocalDateTime end = LocalDateTime.of(2022, 2, 22, 8, 30, 50);
String date = GetDateUtils.getMinSecond(start, end);
System.out.println(date); // 输出 1510:20 即相差1510分钟20秒
1.4 根据传入时分秒(分秒)格式00:00:00(00:00)转换为秒数
LocalDateTime start = LocalDateTime.of(2022, 2, 21, 7, 20, 30);
LocalDateTime end = LocalDateTime.of(2022, 2, 22, 8, 30, 50);
String date = GetDateUtils.getHMS(start, end);
System.out.println(date); // 输出 25:10:20
Long second = GetDateUtils.getSecond(date);
System.out.println("相差秒数:" + second);
控制台打印:
2022-02-21T07:20:30||||2022-02-22T08:30:50
计算两个时间的差:
1645428030||1645518650
差值(秒):90620
相差(xx时xx分xx秒):25:10:20
25:10:20
90620
相差秒数:90620
2. 工具类
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import lombok.extern.slf4j.Slf4j;
/**
* @Description: 时间转换工具类
* @Author: 周兆宇
* @Date: 2022-02-22 08:35:13
*/
@Slf4j
public class GetDateUtils {
/**
* 功能描述: 计算两个时间的时间差 (天时分秒)
*
* @param startTime
* @param endTime
* @return java.lang.String 返回 天时分秒格式
* @author 周兆宇
* @date 2022-02-22 09:36:14
*/
public static String getDate(LocalDateTime startTime, LocalDateTime endTime) {
log.info(startTime + "||||" + endTime);
log.info("计算两个时间的差:");
//获取秒数
long nowSecond = startTime.toEpochSecond(ZoneOffset.ofHours(0));
long endSecond = endTime.toEpochSecond(ZoneOffset.ofHours(0));
long absSeconds = Math.abs(nowSecond - endSecond);
log.info(nowSecond + "||" + endSecond);
//获取秒数
long s = absSeconds % 60;
//获取分钟数
long m = absSeconds / 60 % 60;
//获取小时数
long h = absSeconds / 60 / 60 % 24;
//获取天数
long d = absSeconds / 60 / 60 / 24;
log.info(d + "天" + h + "时" + m + "分" + s + "秒");
return d + "天" + h + "时" + m + "分" + s + "秒";
}
/**
* 功能描述: 计算两个时间的时间差 (时分秒)
*
* @param startTime
* @param endTime
* @return java.lang.String 返回 00:00:00 格式的时分秒差值
* @author 周兆宇
* @date 2022-02-22 09:28:55
*/
public static String getHMS(LocalDateTime startTime, LocalDateTime endTime) {
log.info(startTime + "||||" + endTime);
log.info("计算两个时间的差:");
//获取秒数
long nowSecond = startTime.toEpochSecond(ZoneOffset.ofHours(0));
long endSecond = endTime.toEpochSecond(ZoneOffset.ofHours(0));
long absSeconds = Math.abs(nowSecond - endSecond);
log.info(nowSecond + "||" + endSecond);
log.info("差值(秒):" + absSeconds);
//获取秒数
long s = absSeconds % 60;
String second = String.valueOf(s);
//获取分钟数
long m = absSeconds / 60 % 60;
String minute = String.valueOf(m);
//获取小时数
long h = absSeconds / 60 / 60;
String hour = String.valueOf(h);
// 统一格式为 00:00:00
second = second.length() == 1 ? "0" + second : second;
minute = minute.length() == 1 ? "0" + minute : minute;
hour = hour.length() == 1 ? "0" + hour : hour;
log.info(hour + ":" + minute + ":" + second);
return hour + ":" + minute + ":" + second;
}
/**
* 功能描述: 计算两个时间的时间差 (分秒)
*
* @param startTime
* @param endTime
* @return java.lang.String 返回 00:00 格式的分秒 差值
* @author 周兆宇
* @date 2022-02-22 09:28:55
*/
public static String getMinSecond(LocalDateTime startTime, LocalDateTime endTime) {
log.info(startTime + "||||" + endTime);
log.info("计算两个时间的差:");
//获取秒数
long nowSecond = startTime.toEpochSecond(ZoneOffset.ofHours(0));
long endSecond = endTime.toEpochSecond(ZoneOffset.ofHours(0));
long absSeconds = Math.abs(nowSecond - endSecond);
log.info(nowSecond + "||" + endSecond);
log.info("差值(秒):" + absSeconds);
//获取秒数
long s = absSeconds % 60;
String second = String.valueOf(s);
//获取分钟数
long m = absSeconds / 60;
String minute = String.valueOf(m);
// 统一格式为 00:00:00
second = second.length() == 1 ? "0" + second : second;
minute = minute.length() == 1 ? "0" + minute : minute;
log.info(minute + ":" + second);
return minute + ":" + second;
}
/**
* 功能描述: 时分秒(分秒)格式00:00:00(00:00)转换秒数
*
* @param time 时分秒(分秒)格式00:00:00(00:00)
* @return long 秒数
* @author 周兆宇
* @date 2022-02-22 09:08:48
*/
public static Long getSecond(String time) {
long seconds = 0;
// 时分秒格式00:00:00
if (time.length() == 8) {
int index1 = time.indexOf(":");
int index2 = time.indexOf(":", index1 + 1);
// 小时
seconds = Integer.parseInt(time.substring(0, index1)) * 3600L;
// 分钟
seconds += Integer.parseInt(time.substring(index1 + 1, index2)) * 60L;
// 秒
seconds += Integer.parseInt(time.substring(index2 + 1));
}
// 分秒格式00:00
if (time.length() == 5) {
// 秒 后两位肯定是秒
seconds = Integer.parseInt(time.substring(time.length() - 2));
// 分钟
seconds += Integer.parseInt(time.substring(0, 2)) * 60L;
}
log.info(String.valueOf(seconds));
return seconds;
}
}