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