public class DateUbodyUtil {
/**
* 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 计算天数,计算到小时
*/
public static LocalDateTime calculationDays(LocalDateTime localDateTime, Double day) {
if (day == 0.0) return localDateTime;
int i = 0;
return doCalculationDays(localDateTime, i, day);
}
private static LocalDateTime doCalculationDays(LocalDateTime localDateTime, int i, Double day) {
String dayStr = day.toString();
String[] daysSplit = dayStr.split("\\.");
String integerBit = daysSplit[0];
String decimalStr = daysSplit[1];
if (Double.parseDouble(integerBit) == 0.0 || i == 3) {
return localDateTime;
}
switch (i) {
case 0:
day = Double.parseDouble("0." + decimalStr) * 24;
localDateTime = localDateTime.plusDays(Long.parseLong(integerBit));
break;
case 1:
day = Double.parseDouble("0." + decimalStr) * 60;
localDateTime = localDateTime.plusHours(Long.parseLong(integerBit));
break;
case 2:
day = Double.parseDouble("0." + decimalStr) * 60;
localDateTime = localDateTime.plusMinutes(Long.parseLong(integerBit));
break;
case 3:
day = Double.parseDouble("0." + decimalStr) * 60;
localDateTime = localDateTime.plusSeconds(Long.parseLong(integerBit));
break;
}
return doCalculationDays(localDateTime, ++i, day);
}
public static String localDateTimeToStr(LocalDateTime localDateTime) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(DateUbodyUtil.DATE_TIME_PATTERN);
return df.format(localDateTime);
}
public static void main(String[] args) {
LocalDateTime localDateTime = calculationDays(LocalDateTime.now(), 1.555);
System.out.println(localDateTimeToStr(localDateTime));
}
}
LocalDateTime 输入天数,天数是浮点数,折算成天,时,分,秒计算到LocalDateTime
最新推荐文章于 2024-08-05 17:26:17 发布