package com.voole.platform.util;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
public class DateUtils {
private final static String DATE_TIME_FORMAT = "yyyymmddhhmmss";
private final static String DATE_TIME_FORMAT_1 = "yyyy-MM-dd HH:mm:ss";
/**
* 计算起始日期间的相隔天数
* @param starttime
* @param endtime
* @return
*/
public static long getTimeDayDiff(Date starttime, Date endtime){
long nd = 1000 * 24 * 60 * 60;
long diff = endtime.getTime() - starttime.getTime();
long day = diff / nd;
return day;
}
/**
* 获取给定日期增加给定天数后的日期
* @param date
* @param hour
* @return
*/
public static Date addHour(Date date, int hour){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
long millis = calendar.getTimeInMillis() + ((long) hour) * 3600 * 1000;
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
/**
*
*
Title: getDateNow
*
Description: 获取当前时间 格式yyyymmddhhmmss
* @return
* @author 冯浩 2019年2月25日 下午5:36:56
*/
public static String getDateNow() {
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
return date.format(ofPattern);
}
/**
*
*
Title: format
*
Description: 时间格式化
* @param date
* @return
* @author 冯浩 2019年3月7日 上午11:10:05
*/
public static String format(Date date) {
DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT_1);
LocalDateTime tranfer = tranfer(date);
return tranfer.format(ofPattern);
}
/**
*
*
Title: tranfer
*
Description: 时间类型转换
* @param date
* @return
* @author 冯浩 2019年3月7日 上午11:10:33
*/
public static LocalDateTime tranfer(Date date) {
Instant instant = date.toInstant();
ZoneId systemDefault = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, systemDefault);
}
/**
*
*
Title: betweenTimes
*
Description: 求两个时间之间的差值(秒)
* @param start
* @param end
* @return
* @author 冯浩 2019年3月7日 上午11:27:32
*/
public static long betweenTimes(Date start,Date end) {
LocalDateTime starttime = tranfer(start);
LocalDateTime endtime = tranfer(end);
return ChronoUnit.SECONDS.between(starttime, endtime);
}
/**
*
*
Title: isBefore
*
Description: 两个时间比较
* @param start
* @param end
* @return
* @author 冯浩 2019年3月7日 上午11:33:20
*/
public static boolean isBefore(Date start,Date end) {
LocalDateTime starttime = tranfer(start);
LocalDateTime endtime = tranfer(end);
return starttime.isBefore(endtime);
}
@Test
public void test() {
Date addHour = DateUtils.addHour(new Date(), 40);
//long betweenTimes = DateUtils.betweenTimes(new Date(), addHour);
boolean before = DateUtils.isBefore(new Date(), addHour);
System.out.println(before);
}
}
/**
*
*
Title: addMinutes
*
Description: 当前时间添加分钟
* @param minutes
* @return
* @author 冯浩 2019年3月7日 下午7:12:24
*/
public static Date addMinutes(int minutes) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime plusMinutes = now.plusMinutes(minutes);
return localDateTime2Date(plusMinutes);
}
/**
*
*
Title: localDateTime2Date
*
Description: localdatetime转Date
* @param time
* @return
* @author 冯浩 2019年3月7日 下午7:12:45
*/
public static Date localDateTime2Date(LocalDateTime time) {
ZoneId systemDefault = ZoneId.systemDefault();
ZonedDateTime atZone = time.atZone(systemDefault);
return Date.from(atZone.toInstant());
}
public staticLocalDate conversion(String date){returnLocalDate.parse(date,
DateTimeFormatter.ofPattern(DATE));
}