美文网首页
日拱一卒:计算时间差

日拱一卒:计算时间差

作者: Tinyspot | 来源:发表于2024-01-23 19:50 被阅读0次

1. 计算小时差

public static final Long ONE_HOUR_TIME_LONG = 60 * 60 * 1000L;

@Test
public void hours() throws ParseException {
    Date date1 = new Date();
    // org.apache.commons.lang3.time.DateUtils;
    Date date2 = DateUtils.parseDate("2024-01-24 18:35:00", "yyyy-MM-dd HH:mm:ss");

    long times = date1.getTime() - date2.getTime();
    double hours = times / (ONE_HOUR_TIME_LONG * 1.0);
    Double divide = divide(times, ONE_HOUR_TIME_LONG, 2);
    System.out.println(hours + ", " + divide);
}
/**
 * 除法运算
 *
 * @param dividend 被除数,分子
 * @param divisor 除数,分母
 * @param scale 精度,小数位
 */
public static Double divide(Long dividend, Long divisor, Integer scale) {
    if (dividend == null || divisor == null || divisor == 0L) {
        return 0D;
    }

    BigDecimal b1 = BigDecimal.valueOf(dividend);
    BigDecimal b2 = BigDecimal.valueOf(divisor);
    return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}

1.2 百分数

System.out.println("percent: " + toPercent(1.234));

public static String toPercent(Double value) {
    DecimalFormat decimalFormat = new DecimalFormat("##%");
    return decimalFormat.format(value);
}

2. 计算时间差

@Test
public void duration() throws ParseException {
    Date date1 = new Date();
    Date date2 = DateUtils.parseDate("2024-01-23 18:35:00", "yyyy-MM-dd HH:mm:ss");
    long times = date1.getTime() - date2.getTime();

    long day = times / (24 * 60 * 60 * 1000);
    long hour = (times / (60 * 60 * 1000) - day * 24);
    long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
    long s = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
    System.out.println(day + "天" + hour + "小时" + min + "分" + s + "秒");
}

打印结果:
1天1小时11分14秒

3. 计算日期

@Test
public void beforeDate() {
    String before = DateUtil.beforeDate("2024-01-23", "2024-01-25");
    System.out.println(before);
}
public class DateUtil {
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 调用示例:DateUtil.beforeDate("2024-01-23", "2024-01-25")
     */
    public static String beforeDate(String dateStartStr, String dateEndStr) {
        Date date = parseDateBefore(dateStartStr, dateEndStr);
        return parseDate(date);
    }

    public static Date parseDateBefore(String dateStartStr, String dateEndStr) {
        try {
            Date dateStart = DateUtils.parseDate(dateStartStr, DEFAULT_DATE_FORMAT);
            Date dateEnd = DateUtils.parseDate(dateEndStr, DEFAULT_DATE_FORMAT);

            long times = dateEnd.getTime() - dateStart.getTime();
            Date beforeDate = new Date(dateStart.getTime() - times);
            return beforeDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String parseDate(Date date) {
        return parseDateByFormat(date, DEFAULT_DATE_FORMAT);
    }

    private static String parseDateByFormat(Date date, String dateFormat) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(date);
    }

}

相关文章

网友评论

      本文标题:日拱一卒:计算时间差

      本文链接:https://www.haomeiwen.com/subject/xihpodtx.html