时间工具类

package com.pistonint.idata.common.util;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @Description:
 * @Author:Jiahong Chen
 * @Date: Created in 16:45 2019/3/1
 * @Modified By:
 */
public class DateUtil
{
    public static final DateTimeFormatter YYYY_MM_DD_TIME = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    public static final DateTimeFormatter YYYYMMDDHHMMSS = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    public static final DateTimeFormatter YYYY_MM_DD_DATE = DateTimeFormat.forPattern("yyyy-MM-dd");

    public static final DateTimeFormatter YYYY_MM = DateTimeFormat.forPattern("yyyy-MM");

    public static final DateTimeFormatter RECORD_TIME = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    public static final DateTimeFormatter YYYYMMDDHHMMSS_DATE = DateTimeFormat.forPattern("yyyyMMddHHmmss");

    /**
     * 时间差值(h)
     * @param beginTime
     * @param endTime
     * @return
     */
    public static Float timeMinus(String beginTime, String endTime) {

        DateTime beginDate, endDate;
        if (beginTime.contains("-")) {
            beginDate = YYYY_MM_DD_TIME.parseDateTime(beginTime);
        } else {
            beginDate = YYYYMMDDHHMMSS.parseDateTime(beginTime);
        }

        if (endTime.contains("-")) {
            endDate = YYYY_MM_DD_TIME.parseDateTime(endTime);
        } else {
            endDate = YYYYMMDDHHMMSS.parseDateTime(endTime);
        }

        return new BigDecimal(new Duration(beginDate, endDate).getStandardMinutes()/60.0).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public static int daysBetween(String startDate, String endDate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(startDate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endDate));
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值