|
@@ -0,0 +1,333 @@
|
|
|
+package com.book.dao.utils;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.DayOfWeek;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.temporal.ChronoField;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.time.temporal.TemporalAdjuster;
|
|
|
+import java.time.temporal.TemporalAdjusters;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.TimeZone;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 时间工具类
|
|
|
+ * @author caosongsong
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class TimeUtil {
|
|
|
+
|
|
|
+ public static final String YYYYMMDD1 = "yyyyMMdd";
|
|
|
+
|
|
|
+ public static final String YYYY_MM = "yyyy-MM";
|
|
|
+ public static final String _YYYY_MM = "yyyy_MM";
|
|
|
+ public static final String YYYY = "yyyy";
|
|
|
+
|
|
|
+ public static final String YYYY_MM_DD = "yyyy-MM-dd";
|
|
|
+
|
|
|
+ public static final String YYYYMMDD = "yyyy.mm.dd";
|
|
|
+ public static final String YYYYMMDD_N = "yyyyMMdd";
|
|
|
+
|
|
|
+ public static final String HH_MM_SS = "HH:mm:ss";
|
|
|
+ public static final String HHMMSS = "HHmmss";
|
|
|
+
|
|
|
+ public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
|
|
+
|
|
|
+ public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
|
|
|
+
|
|
|
+ public static final String YYYYMMDDHHMMSS="yyyyMMddHHmmss";
|
|
|
+
|
|
|
+ public static final String YYYYMMDDHHMM="yyyyMMddHHmm";
|
|
|
+
|
|
|
+ public static final String YYYY_MM_DD_HH_MM_SS_Z = "yyyy-MM-dd HH:mm:ss z";
|
|
|
+ public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
|
|
|
+
|
|
|
+ public static final String MMDDHHMMSS = "MMddHHmmss";
|
|
|
+
|
|
|
+ public static final String YYYY_M_DD_HH_MM_SS = "yyyy-M-dd HH:mm:ss";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 获取当前时间的字符串格式
|
|
|
+ * @param format
|
|
|
+ * String 格式化样式"yyyy-MM-dd HH:mm:ss"
|
|
|
+ * @return String 时间字符串
|
|
|
+ */
|
|
|
+ public static String getCurrentTimestamp(String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取昨天的日期或者固定前几日的或者未来几日的date
|
|
|
+ * @param dateNo 数字 ,-1表示昨天;0今天;1明天;2后天
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date getYesterday(int dateNo){
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.add(Calendar.DATE, dateNo);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 获取当前时间戳
|
|
|
+ * @return Timestamp 当前时间
|
|
|
+ */
|
|
|
+ public static Timestamp getCurrentTimestamp() {
|
|
|
+ return new Timestamp(new Date().getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 获取当前日期字符串
|
|
|
+ * @param format
|
|
|
+ * String yyyy-MM-dd
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public static String getCurrentDate(String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+ public static Integer getCurrentIntDate(String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return Integer.valueOf(sdf.format(new Date()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 格式化时间日期
|
|
|
+ * @param date
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String dateToStr(Date date, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 字符串转日期
|
|
|
+ * @param date
|
|
|
+ * @param format
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static Date dateFromStr(String date, String format) throws ParseException{
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ return sdf.parse(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 日期比较
|
|
|
+ * @param date1
|
|
|
+ * @param date2
|
|
|
+ * @return {[ 0 - d1等于d2 ]/[ 1 - d1 大于 d2 ]/[ -1 - d1 小于 d2 ]}
|
|
|
+ */
|
|
|
+ public static int compareDate(Date date1, Date date2) {
|
|
|
+ // 参数检查
|
|
|
+ if (date1 == null || date2 == null) {
|
|
|
+ throw new IllegalArgumentException("非法参数异常,参数不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ return date1.compareTo(date2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 只比较时分秒
|
|
|
+ * @param date1
|
|
|
+ * @param date2
|
|
|
+ * @return {[ 0 - d1等于d2 ]/[ 1 - d1 大于 d2 ]/[ -1 - d1 小于 d2 ]}
|
|
|
+ */
|
|
|
+ public static int compareTime(Date date1, Date date2) {
|
|
|
+ // 参数检查
|
|
|
+ if (date1 == null || date2 == null) {
|
|
|
+ throw new IllegalArgumentException("非法参数异常,参数不能为空!");
|
|
|
+ }
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(date1);
|
|
|
+ int time1 = cal1.get(Calendar.HOUR_OF_DAY) * 60 * 60
|
|
|
+ + cal1.get(Calendar.MINUTE) * 60 + cal1.get(Calendar.SECOND);
|
|
|
+
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(date2);
|
|
|
+ int time2 = cal2.get(Calendar.HOUR_OF_DAY) * 60 * 60
|
|
|
+ + cal2.get(Calendar.MINUTE) * 60 + cal2.get(Calendar.SECOND);
|
|
|
+
|
|
|
+ int tmp = time1 - time2;
|
|
|
+ if (tmp > 0) {
|
|
|
+ return 1;
|
|
|
+ } else if (tmp == 0) {
|
|
|
+ return 0;
|
|
|
+ } else {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @todo 获取日期的星期几
|
|
|
+ * @param date
|
|
|
+ * @return int {1- 星期日 / 2 -星期一 / 7-星期六}
|
|
|
+ */
|
|
|
+ public static int getDateOfWeek(Date date) {
|
|
|
+ // 参数检查
|
|
|
+ if (date == null) {
|
|
|
+ throw new IllegalArgumentException("非法参数异常,参数不能为空!");
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ return calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 当前日期的星期
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int currentDayInWeek() {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+ return calendar.get(Calendar.DAY_OF_WEEK);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date firstTime(Date curDate) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(curDate);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date lastTime(Date curDate) {
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(curDate);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ return calendar.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 日、时、分、秒的间距
|
|
|
+ * @param valueDate
|
|
|
+ * @param begin
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int interval(Date valueDate,Date begin,Date end){
|
|
|
+ if(begin.getTime() > end.getTime()){
|
|
|
+ throw new IllegalArgumentException("begin or end is invalid");
|
|
|
+ }
|
|
|
+ if(valueDate.getTime() < begin.getTime()){
|
|
|
+ return -1;
|
|
|
+ }else if(valueDate.getTime()>= begin.getTime() && valueDate.getTime() <= end.getTime()){
|
|
|
+ return 0;
|
|
|
+ }else{
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 时、分、秒的间距
|
|
|
+ * @param valueDate
|
|
|
+ * @param begin
|
|
|
+ * @param end
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static int intervalItime(Date valueDate,Date begin,Date end){
|
|
|
+ if(compareTime(end, begin) == -1){
|
|
|
+ throw new IllegalArgumentException("begin or end is invalid");
|
|
|
+ }
|
|
|
+ if(compareTime(valueDate, begin) == -1){
|
|
|
+ return -1;
|
|
|
+ }else if(compareTime(valueDate, begin) == 1 && compareTime(end, valueDate) == 1){
|
|
|
+ return 0;
|
|
|
+ }else{
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前一天的工作日
|
|
|
+ * @param format 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getBeforeWorkDay(String format) {
|
|
|
+ TemporalAdjuster temporalAdjuster = nextWorkingDay();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
+ return LocalDate.now().with(temporalAdjuster).format(formatter);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定日期前一个工作日
|
|
|
+ * @param year 年
|
|
|
+ * @param month 月
|
|
|
+ * @param dayOfMonth 日
|
|
|
+ * @param format 日期格式
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getBeforeWorkDay(int year, int month, int dayOfMonth, String format) {
|
|
|
+ TemporalAdjuster temporalAdjuster = nextWorkingDay();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
+ return LocalDate.of(year,month,dayOfMonth).with(temporalAdjuster).format(formatter);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static TemporalAdjuster nextWorkingDay() {
|
|
|
+ return TemporalAdjusters.ofDateAdjuster(
|
|
|
+
|
|
|
+ temporal -> {
|
|
|
+
|
|
|
+ DayOfWeek dow =
|
|
|
+
|
|
|
+ DayOfWeek.of(temporal.get(ChronoField.DAY_OF_WEEK));
|
|
|
+
|
|
|
+ int dayToAdd = -1;
|
|
|
+
|
|
|
+ if (dow ==DayOfWeek.MONDAY){
|
|
|
+
|
|
|
+ dayToAdd = -3;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dow ==DayOfWeek.SATURDAY){
|
|
|
+
|
|
|
+ dayToAdd = -1;
|
|
|
+
|
|
|
+ }
|
|
|
+ if (dow ==DayOfWeek.SUNDAY){
|
|
|
+
|
|
|
+ dayToAdd = -2;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return temporal.plus(dayToAdd, ChronoUnit.DAYS);
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ public static String getDateString(long time){
|
|
|
+ Date date = new Date(time);
|
|
|
+ return dateToStr(date,YYYY_MM_DD);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getPointDateString(long time){
|
|
|
+ Date date = new Date(time);
|
|
|
+ return dateToStr(date,YYYYMMDD);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getTimeString(long time){
|
|
|
+ Date date = new Date(time);
|
|
|
+ return dateToStr(date,HH_MM_SS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Date dateFromStringWithTimezoneEtc_GMT(String datestr){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
|
|
|
+ try {
|
|
|
+ sdf.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
|
|
|
+ return sdf.parse(datestr);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|