private static String convertToDaysOrMonths(long minutes) {
Duration duration = Duration.ofMinutes(minutes);
LocalDate start = LocalDate.of(1970, 1, 1);
LocalDateTime endDateTime = LocalDateTime.of(start, LocalDateTime.now().toLocalTime());
LocalDateTime startDateTime = endDateTime.minus(duration);
// 使用ChronoUnit.DAYS.between计算相对天数
long days = Duration.between(startDateTime, endDateTime).toDays();
// 使用当前月份的天数判断
int currentMonthDays = LocalDate.now().lengthOfMonth();
// 如果相对天数大于等于当前月份的天数,返回月数;否则返回相对天数
if (days >= currentMonthDays) {
long months = days / currentMonthDays;
long remainingDays = days % currentMonthDays;
if (remainingDays == 0){
return months + " 月 ";
}
return months + " 月 " + remainingDays + " 天";
} else {
return days + " 天";
}
}
Java传入分钟值计算天数或者月数实现方法
最新推荐文章于 2024-05-21 10:23:42 发布