LocalDateTime 输入天数,天数是浮点数,折算成天,时,分,秒计算到LocalDateTime

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));
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值