Java 时间间隔计算工具类

Java 时间间隔计算工具类

一、工具类—DateIntervalUtil

package com.example.demotest.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author wanglin
 * @version 1.0
 * @date 2022-05-10 周二
 */
public class DateIntervalUtil {
    /**
     * 计算2个时间相差的天数、小时、分钟、秒
     *
     * @param startTime 开始时间
     * @param endTime   截止时间
     * @param format    时间格式 yyyy-MM-dd HH:mm:ss
     * @param str       返回的数据为:day-天、hour-小时、min-分钟、second-秒
     * @return
     */
    public static Long dateDiff(String startTime, String endTime, String format, String str) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        // 一天的毫秒数
        long nd = 1000 * 24 * 60 * 60;
        // 一小时的毫秒数
        long nh = 1000 * 60 * 60;
        // 一分钟的毫秒数
        long nm = 1000 * 60;
        // 一秒钟的毫秒数
        long ns = 1000;
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long second = 0;
        // 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            // 计算差多少天
            day = diff / nd;
            // 计算差多少小时
            hour = diff / nh;
            // 计算差多少分钟
            min = diff / nm;
            // 计算差多少秒
            second = diff / ns;
            // 输出结果
            System.out.println("时间相差:" + day + "天" +
                    (hour - day * 24) + "小时"
                    + (min - day * 24 * 60) + "分钟" +
                    second + "秒。");
            /*System.out.println("hour=" + hour + ",min=" + min);*/
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (str.equalsIgnoreCase("day")) {
            return day;
        } else if (str.equalsIgnoreCase("hour")) {
            return hour;
        } else if (str.equalsIgnoreCase("min")) {
            return min;
        } else {
            return second;
        }
    }

    public static void main(String[] args) {
        System.out.println("相差多少天:" + dateDiff(
                "2022-04-18 11:31:25",
                "2022-04-19 12:31:25",
                "yyyy-MM-dd HH:mm:ss",
                "day"));
                
        System.out.println("相差多少小时:" + dateDiff(
                "2022-04-30 11:31:25",
                "2022-05-01 11:51:25",
                "yyyy-MM-dd HH:mm:ss",
                "hour"));
                
        System.out.println("相差多少分钟:" + dateDiff(
                "2022-04-18 11:31:25",
                "2022-04-18 12:51:25",
                "yyyy-MM-dd HH:mm:ss",
                "min"));

        System.out.println("相差多少秒:" + dateDiff(
                "2022-04-18 11:31:25",
                "2022-04-18 12:31:25",
                "yyyy-MM-dd HH:mm:ss",
                "sec"));

        Long min = dateDiff(
                "2022-04-18 11:11:25",
                "2022-04-18 11:51:25",
                "yyyy-MM-dd HH:mm:ss",
                "min");

        System.out.println("总分钟数:" + min);
        Float h = (float) min / 60;
        System.out.println(h);

        double num1 = 0.55624563;
        DecimalFormat df = new DecimalFormat("0%");
//        System.out.println(df.format(num1));  // 输出52%

        df = new DecimalFormat("0.0");
//        System.out.println(df.format(num1));  // 输出0.52

        System.out.println(df.format((float) min / 60));
    }
}

二、测试结果

在这里插入图片描述

关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java版IRR计算工具类的示例代码: ```java import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.exception.TooManyIterationsException; public class IRRCalculator { /** * 计算IRR * @param cashFlows 现金流数组,第一个元素是负值,表示投资的现金流出;其余元素是正值,表示现金流入。 * @return IRR(内部收益率) */ public static double calculateIRR(double[] cashFlows) { UnivariateFunction f = new UnivariateFunction() { @Override public double value(double x) { double npv = 0.0; for (int i = 0; i < cashFlows.length; i++) { npv += cashFlows[i] / Math.pow(1 + x, i); } return npv; } }; BrentSolver solver = new BrentSolver(); solver.setAbsoluteAccuracy(1e-10); solver.setMaximalIterationCount(1000); try { return solver.solve(f, -1.0, 1.0); } catch (TooManyIterationsException e) { return Double.NaN; } } /** * 测试 */ public static void main(String[] args) { double[] cashFlows = {-1000.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0}; double irr = calculateIRR(cashFlows) * 100; System.out.println("IRR: " + irr + "%"); } } ``` 在这个示例代码中,我们定义了一个静态方法`calculateIRR`,它接受一个现金流数组作为参数,并返回IRR。我们使用了Apache Commons Math库中的`UnivariateFunction`和`BrentSolver`来计算IRR,其中`UnivariateFunction`表示一元函数,`BrentSolver`表示一个非线性方程求解器。在`calculateIRR`方法中,我们首先定义了一个一元函数`f`,它根据输入的IRR计算现金流的净现值(NPV)。然后,我们使用`BrentSolver`来求解方程`f(x) = 0`,其中`x`就是IRR。最后,我们在`main`方法中使用示例数据来测试`calculateIRR`方法,计算出IRR并打印出来。 需要注意的是,在现实中,现金流一般是以年为单位计算的。如果现金流是以月、季度或其他时间间隔计算的,我们需要根据实际情况将其转换为年。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值