From c635d78b479510ebe2556a420948effcd30a0731 Mon Sep 17 00:00:00 2001 From: skyouc Date: 星期六, 21 十二月 2024 18:40:43 +0800 Subject: [PATCH] 新建德森项目分支 --- zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/DateUtils.java | 430 ++++++++++++++++++++++++++-------------------------- 1 files changed, 215 insertions(+), 215 deletions(-) diff --git a/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/DateUtils.java b/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/DateUtils.java index 471ac2c..565ab70 100644 --- a/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/DateUtils.java +++ b/zy-asrs-framework/src/main/java/com/zy/asrs/framework/common/DateUtils.java @@ -1,215 +1,215 @@ -package com.zy.asrs.framework.common; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Objects; -import java.util.concurrent.TimeUnit; - -/** - * 鏃堕棿宸ュ叿绫� - * Created by vincent on 2019-04-15 - */ -public class DateUtils { - - public final static String yyyyMMdd_C="yyyy骞碝M鏈坉d鏃�"; - public final static String yyyyMM_F="yyyy-MM"; - public final static String yyyyMMdd_F="yyyy-MM-dd"; - public final static String yyyyMMddHHmmss_F="yyyy-MM-dd HH:mm:ss"; - public final static String yyyyMMddHHmmsssss_F="yyyy-MM-dd HH:mm:ss,SSS"; - public final static String yyyy="yyyy"; - public final static String yyyyMM="yyyyMM"; - public final static String yyyyMMdd="yyyyMMdd"; - public final static String yyyyMMddHH="yyyyMMddHH"; - public final static String yyyyMMddHHmmss="yyyyMMddHHmmss"; - public final static String YYMMDDHHMMSS="YYMMDDHHMMSS"; - public final static String yyyyMMddHHmmsssss="yyyyMMddHHmmssSSS"; - - /** - * date ==>> string - */ - public static String convert(Date date, String pattern){ - return new SimpleDateFormat(pattern).format(date); - } - - public static String convert(Date date){ - return convert(date, yyyyMMddHHmmss_F); - } - - /** - * string ==>> date - */ - public static Date convert(String str, String pattern){ - if (str.length() < pattern.length()){ - throw new RuntimeException("鏃堕棿瑙f瀽澶辫触 ==>> "+str); - } - if (str.length() > pattern.length()){ - str = str.substring(0, pattern.length()); - } - SimpleDateFormat format = new SimpleDateFormat(pattern); - Date date; - try { - date = format.parse(str); - } catch (ParseException e) { - throw new RuntimeException("鏃堕棿瑙f瀽澶辫触 ==>> "+str); - } - return date; - } - - public static Date convert(String str){ - return convert(str, yyyyMMddHHmmss_F); - } - - /** - * 涓や釜date涔嬮棿鐩稿樊鐨勫ぉ鏁帮紝涓嶆弧涓�澶╃畻涓�澶� - */ - public static int diff(Date date1, Date date2){ - return getDaysByTimestamp(Math.abs(date2.getTime() - date1.getTime())); - } - - public static long diffToMinute(Date date1, Date date2){ - return Math.abs(date2.getTime() - date1.getTime())/1000/60; - } - - public static long diffToSeconds(Date date1, Date date2){ - return Math.abs(date2.getTime() - date1.getTime())/1000; - } - - private static int getDaysByTimestamp(long timestamp){ - double daysPoint = Arith.divides(2, timestamp, (1000 * 3600 * 24)); - int daysPoint1 = (int) daysPoint; - double daysPoint2 = (double) daysPoint1; - if (daysPoint > daysPoint2){ - return daysPoint1 + 1; - } - return daysPoint1; - } - - /** - * 鍏ュ弬date璺濈鐜板湪鐨勭鏁� - */ - public static int diffToNow(Date date){ - long diff = new Date().getTime() - date.getTime(); - return (int) (Math.abs(diff) / 1000); - } - - /** - * 褰撳墠鏃堕棿鎴筹紙鍗曚綅锛氱锛� - */ - public static String createTimeStamp() { - return Long.toString(System.currentTimeMillis() / 1000); - } - - /** - * 鏃堕棿璁$畻鍑芥暟 - * @param date 琚绠楁椂闂村疄渚� - * @param val 璁$畻鍊� - * @param timeUnit 璁$畻鍊煎崟浣� - * @param subtraction 鍑忔硶甯冨皵 true锛氬綋鍓嶅嚱鏁颁负鍑忔硶璁$畻锛宖alse锛氬弽涔� - * @return 璁$畻缁撴灉 Date - */ - public static Date calculate(Date date, Long val, TimeUnit timeUnit, boolean subtraction){ - if (Objects.isNull(date) || Objects.isNull(val) || Objects.isNull(timeUnit)){ - return null; - } - return new Date(subtraction?date.getTime()-timeUnit.toMillis(val):date.getTime()+timeUnit.toMillis(val)); - } - - public static Date calculate(Date date, Long val, TimeUnit timeUnit){ - return calculate(date, val, timeUnit, false); - } - - public static Date getStartOfDay(Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - calendar.set(Calendar.HOUR_OF_DAY, 0); - calendar.set(Calendar.MINUTE, 0); - calendar.set(Calendar.SECOND, 0); - calendar.set(Calendar.MILLISECOND, 0); - return calendar.getTime(); - } - - public static Date getEndOfDay(Date date) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(date); - calendar.set(Calendar.HOUR_OF_DAY, 23); - calendar.set(Calendar.MINUTE, 59); - calendar.set(Calendar.SECOND, 59); - calendar.set(Calendar.MILLISECOND, 999); - return calendar.getTime(); - } - - /** - * 鏃堕棿瀵硅薄DateEntity - */ - public static DateEntity getDateEntity(Date date){ - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - DateEntity dateEntity = new DateEntity(); - dateEntity.setYear(cal.get(Calendar.YEAR)); - dateEntity.setMonth(cal.get(Calendar.MONTH)); - dateEntity.setDay(cal.get(Calendar.DATE)); - dateEntity.setHour(cal.get(Calendar.HOUR_OF_DAY)); - dateEntity.setMinute(cal.get(Calendar.MINUTE)); - dateEntity.setSecond(cal.get(Calendar.SECOND)); - return dateEntity; - } - - static class DateEntity { - int year; - int month; - int day; - int hour; - int minute; - int second; - - public int getYear() { - return year; - } - - public void setYear(final int year) { - this.year = year; - } - - public int getMonth() { - return month; - } - - public void setMonth(final int month) { - this.month = month + 1; - } - - public int getDay() { - return day; - } - - public void setDay(final int day) { - this.day = day; - } - - public int getHour() { - return hour; - } - - public void setHour(final int hour) { - this.hour = hour; - } - - public int getMinute() { - return minute; - } - - public void setMinute(final int minute) { - this.minute = minute; - } - - public int getSecond() { - return second; - } - - public void setSecond(final int second) { - this.second = second; - } - } -} +package com.zy.asrs.framework.common; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * 鏃堕棿宸ュ叿绫� + * Created by vincent on 2019-04-15 + */ +public class DateUtils { + + public final static String yyyyMMdd_C="yyyy骞碝M鏈坉d鏃�"; + public final static String yyyyMM_F="yyyy-MM"; + public final static String yyyyMMdd_F="yyyy-MM-dd"; + public final static String yyyyMMddHHmmss_F="yyyy-MM-dd HH:mm:ss"; + public final static String yyyyMMddHHmmsssss_F="yyyy-MM-dd HH:mm:ss,SSS"; + public final static String yyyy="yyyy"; + public final static String yyyyMM="yyyyMM"; + public final static String yyyyMMdd="yyyyMMdd"; + public final static String yyyyMMddHH="yyyyMMddHH"; + public final static String yyyyMMddHHmmss="yyyyMMddHHmmss"; + public final static String YYMMDDHHMMSS="YYMMDDHHMMSS"; + public final static String yyyyMMddHHmmsssss="yyyyMMddHHmmssSSS"; + + /** + * date ==>> string + */ + public static String convert(Date date, String pattern){ + return new SimpleDateFormat(pattern).format(date); + } + + public static String convert(Date date){ + return convert(date, yyyyMMddHHmmss_F); + } + + /** + * string ==>> date + */ + public static Date convert(String str, String pattern){ + if (str.length() < pattern.length()){ + throw new RuntimeException("鏃堕棿瑙f瀽澶辫触 ==>> "+str); + } + if (str.length() > pattern.length()){ + str = str.substring(0, pattern.length()); + } + SimpleDateFormat format = new SimpleDateFormat(pattern); + Date date; + try { + date = format.parse(str); + } catch (ParseException e) { + throw new RuntimeException("鏃堕棿瑙f瀽澶辫触 ==>> "+str); + } + return date; + } + + public static Date convert(String str){ + return convert(str, yyyyMMddHHmmss_F); + } + + /** + * 涓や釜date涔嬮棿鐩稿樊鐨勫ぉ鏁帮紝涓嶆弧涓�澶╃畻涓�澶� + */ + public static int diff(Date date1, Date date2){ + return getDaysByTimestamp(Math.abs(date2.getTime() - date1.getTime())); + } + + public static long diffToMinute(Date date1, Date date2){ + return Math.abs(date2.getTime() - date1.getTime())/1000/60; + } + + public static long diffToSeconds(Date date1, Date date2){ + return Math.abs(date2.getTime() - date1.getTime())/1000; + } + + private static int getDaysByTimestamp(long timestamp){ + double daysPoint = Arith.divides(2, timestamp, (1000 * 3600 * 24)); + int daysPoint1 = (int) daysPoint; + double daysPoint2 = (double) daysPoint1; + if (daysPoint > daysPoint2){ + return daysPoint1 + 1; + } + return daysPoint1; + } + + /** + * 鍏ュ弬date璺濈鐜板湪鐨勭鏁� + */ + public static int diffToNow(Date date){ + long diff = new Date().getTime() - date.getTime(); + return (int) (Math.abs(diff) / 1000); + } + + /** + * 褰撳墠鏃堕棿鎴筹紙鍗曚綅锛氱锛� + */ + public static String createTimeStamp() { + return Long.toString(System.currentTimeMillis() / 1000); + } + + /** + * 鏃堕棿璁$畻鍑芥暟 + * @param date 琚绠楁椂闂村疄渚� + * @param val 璁$畻鍊� + * @param timeUnit 璁$畻鍊煎崟浣� + * @param subtraction 鍑忔硶甯冨皵 true锛氬綋鍓嶅嚱鏁颁负鍑忔硶璁$畻锛宖alse锛氬弽涔� + * @return 璁$畻缁撴灉 Date + */ + public static Date calculate(Date date, Long val, TimeUnit timeUnit, boolean subtraction){ + if (Objects.isNull(date) || Objects.isNull(val) || Objects.isNull(timeUnit)){ + return null; + } + return new Date(subtraction?date.getTime()-timeUnit.toMillis(val):date.getTime()+timeUnit.toMillis(val)); + } + + public static Date calculate(Date date, Long val, TimeUnit timeUnit){ + return calculate(date, val, timeUnit, false); + } + + public static Date getStartOfDay(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTime(); + } + + public static Date getEndOfDay(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.set(Calendar.HOUR_OF_DAY, 23); + calendar.set(Calendar.MINUTE, 59); + calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND, 999); + return calendar.getTime(); + } + + /** + * 鏃堕棿瀵硅薄DateEntity + */ + public static DateEntity getDateEntity(Date date){ + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + DateEntity dateEntity = new DateEntity(); + dateEntity.setYear(cal.get(Calendar.YEAR)); + dateEntity.setMonth(cal.get(Calendar.MONTH)); + dateEntity.setDay(cal.get(Calendar.DATE)); + dateEntity.setHour(cal.get(Calendar.HOUR_OF_DAY)); + dateEntity.setMinute(cal.get(Calendar.MINUTE)); + dateEntity.setSecond(cal.get(Calendar.SECOND)); + return dateEntity; + } + + static class DateEntity { + int year; + int month; + int day; + int hour; + int minute; + int second; + + public int getYear() { + return year; + } + + public void setYear(final int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(final int month) { + this.month = month + 1; + } + + public int getDay() { + return day; + } + + public void setDay(final int day) { + this.day = day; + } + + public int getHour() { + return hour; + } + + public void setHour(final int hour) { + this.hour = hour; + } + + public int getMinute() { + return minute; + } + + public void setMinute(final int minute) { + this.minute = minute; + } + + public int getSecond() { + return second; + } + + public void setSecond(final int second) { + this.second = second; + } + } +} -- Gitblit v1.9.1