package cn.com.hongyitech.accountsystem.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
@Description: 新特性的时间处理工具类
@Author: hl
@CreateDate: 2019/1/7 9:33
@UpdateUser: hl
@UpdateDate: 2019/1/7 9:33
@UpdateRemark: 修改内容
@Version: 1.0
适用jdk 8及以上版本
*/
public class DateUtil {
/**
/**
/**
public static String date2YMDhms(Date date) {
return date2YMDhmsss(date);
}
/**
/**
字符串形式日期转换成标准日期 String --> Date “yyyy-MM-dd”–>yyyy-MM-dd
@return
@throws
@author hl
@date 2019/1/7 9:55
*/
public static LocalDate YMDToDate(String date) {
return LocalDate.parse(date, DateTimeFormatter.ofPattern(“yyyy-MM-dd”));
}
/**
/**
/**
/**
}
/**
/**
/**
/**
/**
/**
/**
* 获取指定日期后一星期的字符串格式日期
*
* @return
* @throws
* @author hl
* @date 2019/1/7 12:02
*/
public static String getAfterWeekDateYMDHMS(Date date) {
LocalDateTime localDateTime = DateToLocalDateTime(date);
LocalDateTime AfterWeek = localDateTime.plusDays(7);
return localDateTime2YMDHMS(AfterWeek);
}
/**
* localDateTime2YMDHMS
* 返回的格式为 yyyy-MM-dd HH:mm:ss
*
* @return
* @throws
* @author hl
* @date 2019/1/7 12:04
*/
public static String localDateTime2YMDHMS(LocalDateTime localDateTime) {
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return df.format(localDateTime);
}
/**
* 计算两个日期之间的间隔天数,只取整数部分
* 时间格式必须是yyyy-mm-dd HH:mm:ss
*
* @return
* @throws
* @author hl
* @date 2019/1/7 11:40
*/
public static int checkDaysBegTmAndEndTm(String begTm, String endTm) {
LocalDateTime start = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
LocalDateTime end = localDateTimeParse(endTm, "yyyy-MM-dd HH:mm:ss");
Duration between = Duration.between(start, end);
int betweenDays = (int) between.toDays();
return Math.abs(betweenDays);
}
/**
* 获取两个时间的时间差(小时)
*
* @return
* @throws
* @author hl
* @date 2019/1/7 13:48
*/
public static int getHourFromTwoTime(String begTm, String endTm) {
LocalDateTime start = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
LocalDateTime end = localDateTimeParse(endTm, "yyyy-MM-dd HH:mm:ss");
Duration between = Duration.between(start, end);
int betweenDays = (int) between.toHours();
return Math.abs(betweenDays);
}
/**
* 返回指定时间的零时零分零秒的时间
*
* @param date
* @return yyyy-MM-dd 00:00:00
*/
public static String getBeforOneDateYMDHMS(Date date) {
String str = dateToYMD(date);
LocalDate localDate = LocalDate.parse(str);
LocalDateTime localDateTime = localDate.atStartOfDay();
DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return dateTimeFormater.format(localDateTime);
}
/**
* 返回指定时间的23点59分59秒
*
* @param date
* @return yyyy-MM-dd 00:00:00
*/
public static String getEndOneDateYMDHMS(Date date) {
String str = dateToYMD(date);
LocalDate localDate = LocalDate.parse(str);
LocalDateTime localDateTime = localDate.atStartOfDay();
localDateTime = localDateTime.plusHours(23);
localDateTime = localDateTime.plusMinutes(59);
localDateTime = localDateTime.plusSeconds(59);
DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return dateTimeFormater.format(localDateTime);
}
/**
* 当前时间与begTm的前十分钟的大小
*
* @return 两个时间相隔的毫秒数
* @throws
* @author hl
* @date 2019/1/7 12:47
*/
public static long checkDaysBeforeTenMinuteCurrTmAndBegTm(String toDate, String begTm) {
LocalDateTime localDateTime = localDateTimeParse(begTm, "yyyy-MM-dd HH:mm:ss");
LocalDateTime toDateLocalDateTime = localDateTimeParse(toDate, "yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTimeTenMinuteAfter = localDateTime.plusMinutes(10);
Duration between = Duration.between(toDateLocalDateTime, localDateTimeTenMinuteAfter);
return between.toMillis();
}
/**
* 获取指定日期yyyy-mm-dd HH:mm:ss的前后几个月,前用负数表示2月份前的一个月就是-1
*
* @return
* @throws
* @author hl
* @date 2019/1/7 13:36
*/
public static String getBeforSomeMonthFromCurrent(String currentTm, int month) {
LocalDateTime localDateTime = localDateTimeParse(currentTm, "yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTimeBeforSomeMonth = localDateTime.plusMonths(month);
return localDateTime2YMDHMS(localDateTimeBeforSomeMonth);
}
/**
* 获取当前时间前一个月的日期yyyy-mm-dd HH:mm:ss格式
*
* @return
* @throws
* @author hl
* @date 2019/1/7 13:44
*/
public static String getBeforOneMonthFromCurrent() {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTimeBeforOneMonth = localDateTime.plusMonths(-1);
return localDateTime2YMDHMS(localDateTimeBeforOneMonth);
}
/**
* 获取当前时间后一个月的日期yyyy-mm-dd HH:mm:ss格式
*
* @return
* @throws
* @author hl
* @date 2019/1/7 13:46
*/
public static String getAfterOneMonthFromCurrent() {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTimeAfterOneMonth = localDateTime.plusMonths(1);
return localDateTime2YMDHMS(localDateTimeAfterOneMonth);
}
/**
* 获取当前时间的几个月之前或之后的时间值yyyy-mm-dd HH:mm:ss格式
* 当前时间前传负数,后传正数
*
* @return
* @throws
* @author hl
* @date 2019/1/7 13:58
*/
public static String getBeforeSomeMonthFromCurrent(int month) {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTimeAfterOneMonth = localDateTime.plusMonths(month);
return localDateTime2YMDHMS(localDateTimeAfterOneMonth);
}
/**
* 获取当前时间后N个小时的字符串时间
* yyyy-mm-dd HH:mm:ss
* @author hl
* @return
* @exception
* @date 2019/1/7 15:03
*/
public static String getAfterTimeDateYMDHMS(int i) {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTimeAdd = localDateTime.plusHours(i);
return localDateTime2YMDHMS(localDateTimeAdd);
}
/**
* @param str 需要校验的时间字符串 2018-09-10 或者2018-09-10 12:23:38
* @param pattern 时间的格式 yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss(24小时制)
* @return
*/
public static boolean isValidDate(String str, String pattern) {
boolean convertSuccess = true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}
public static void main(String[] args) {
System.out.println("dateToYMD: " + dateToYMD(new Date()));
System.out.println("dateToYMDHMS: " + dateToYMDHMS(new Date()));
System.out.println("dateToYMDhms: " + dateToYMDhms(new Date()));
System.out.println("date2YMDhmsss: " + date2YMDhmsss(new Date()));
System.out.println("YMDToDate: " + YMDToDate("2019-01-07"));
System.out.println("YMDHMSToDate: " + YMDHMSToDate("2019-01-07 00:00:09"));
System.out.println("DateToLocalDateTime: " + DateToLocalDateTime(new Date()));
System.out.println("DateTime2Date: " + DateTime2Date(LocalDateTime.now()));
System.out.println("LocalDateTimeToLocalDate: " + LocalDateTimeToLocalDate(LocalDateTime.now()));
System.out.println("getTheYear: " + getTheYear());
System.out.println("localDateTimeParse: " + localDateTimeParse("2019-01-07 00:50:00", "yyyy-MM-dd HH:mm:ss"));
System.out.println("checkBwintervalNumberMinBegTmAndEndTm: " + checkBwintervalNumberMinBegTmAndEndTm("2019-01-07 00:00:00", "2019-01-07 00:41:00", 40));
System.out.println("checkBegTmAndEndTm: " + checkBegTmAndEndTm("2019-01-07 00:30:00", "2019-01-07 00:41:00"));
System.out.println("checkDaysBegTmAndEndTm: " + checkDaysBegTmAndEndTm("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
System.out.println("getAfterWeekDateYMDHMS: " + getAfterWeekDateYMDHMS(new Date()));
System.out.println("getBeforOneMonthFromCurrent: " + getBeforSomeMonthFromCurrent("2019-01-07 00:00:50", 1));
System.out.println("getAfterOneMonthFromCurrent: " + getAfterOneMonthFromCurrent());
System.out.println("getBeforOneMonthFromCurrent: " + getBeforOneMonthFromCurrent());
System.out.println("getBeforOneDateYMDHMS: " + getBeforOneDateYMDHMS(new Date()));
System.out.println("getBeforeSomeMonthFromCurrent: " + getBeforeSomeMonthFromCurrent(3));
System.out.println("getHourFromTwoTime: " + getHourFromTwoTime("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
System.out.println("checkBw40MinBegTmAndEndTm: " + checkBw40MinBegTmAndEndTm("2019-01-09 00:00:00", "2019-01-07 00:00:50"));
System.out.println("checkDaysBeforeTenMinuteCurrTmAndBegTm: " + checkDaysBeforeTenMinuteCurrTmAndBegTm("2019-01-07 00:00:50", "2019-01-07 00:00:50"));
System.out.println("getEndOneDateYMDHMS: " + getEndOneDateYMDHMS(new Date()));
System.out.println("isValidDate: " + isValidDate("2019-01-07 00:00:50", "yyyy-MM-dd HH:mm:ss"));
}
}