一、时间换算工具类DateUtils
public class DateUtils {
public static String timeConversion(Date inTime) {
Date curTime = new Date();
long timeDiff = curTime.getTime() - inTime.getTime();
long min = 60 * 1000;
long hour = min * 60;
long day = hour * 24;
long week = day * 7;
long month = day * 30;
long year = month * 12;
DecimalFormat df = new DecimalFormat("#");
double exceedyear = Math.floor(timeDiff / year);
double exceedmonth = Math.floor(timeDiff / month);
double exceedWeek = Math.floor(timeDiff / week);
double exceedDay = Math.floor(timeDiff / day);
double exceedHour = Math.floor(timeDiff / hour);
double exceedMin = Math.floor(timeDiff / min);
if (exceedyear < 100 && exceedyear > 0) {
return df.format(exceedyear) + "年前";
} else {
if (exceedmonth < 12 && exceedmonth > 0) {
return df.format(exceedmonth) + "月前";
} else {
if (exceedWeek <= 4 && exceedWeek > 0) {
return df.format(exceedWeek) + "星期前";
} else {
if (exceedDay < 7 && exceedDay > 0) {
return df.format(exceedDay) + "天前";
} else {
if (exceedHour < 24 && exceedHour > 0) {
return df.format(exceedHour) + "小时前";
} else {
return df.format(exceedMin) + "分钟前";
}
}
}
}
}
}
public static String getDurationTime(long timeDiff) {
Duration duration = Duration.ofMillis(timeDiff);
long days = duration.toDays();
long hours = duration.toHours() % 24;
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() % 60;
String result = "";
if (days > 0) {
result += days + "天";
}
if (hours > 0) {
result += hours + "小时";
}
if (minutes > 0) {
result += minutes + "分钟";
}
if (seconds > 0) {
result += seconds + "秒";
}
if (result.equals("")) {
result = "0秒";
}
return result;
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
return formatter.parse(strDate, pos);
}
public static LocalDateTime isoConvertToLocal(String isoTime) {
if(StringUtils.isBlank(isoTime)){
return null;
}
ZonedDateTime zonedDateTime = ZonedDateTime.parse(isoTime);
ZoneId zoneId = ZoneId.systemDefault();
return zonedDateTime.withZoneSameInstant(zoneId).toLocalDateTime();
}
public static String getDuration(LocalDateTime execStartTime, LocalDateTime execEndTime) {
Duration duration = Duration.between(execStartTime, execEndTime);
long days = duration.toDays();
long hours = duration.toHours() % 24;
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() % 60;
long millis = duration.toMillis() % 1000;
String formattedDuration = String.format("%d天%d小时%d分%d秒%d毫秒", days, hours, minutes, seconds, millis);
return formattedDuration;
}