From 9af47b4c0c25c5c2159ac6ceefced88dec72a2f9 Mon Sep 17 00:00:00 2001 From: baoshiwei <baoshiwei@shlanbao.cn> Date: 星期五, 25 十月 2024 16:58:18 +0800 Subject: [PATCH] Merge branch 'master' into herb-new --- jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java | 1484 ++++++++++++++++++++++++++++++---------------------------- 1 files changed, 760 insertions(+), 724 deletions(-) diff --git a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java index e063da5..427d8c5 100644 --- a/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java +++ b/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java @@ -8,6 +8,8 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -21,743 +23,777 @@ */ public class DateUtils extends PropertyEditorSupport { - public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd"); - } - }; - public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyyMMdd"); - } - }; - public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy骞碝M鏈坉d鏃�"); - } - }; - public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd HH:mm"); - } - }; - public static ThreadLocal<SimpleDateFormat> yyyymmddhhmmss = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyyMMddHHmmss"); - } - }; - public static ThreadLocal<SimpleDateFormat> short_time_sdf = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("HH:mm"); - } - }; - public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() { - @Override - protected SimpleDateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - } - }; - - /** - * 浠ユ绉掕〃绀虹殑鏃堕棿 - */ - private static final long DAY_IN_MILLIS = 24 * 3600 * 1000; - private static final long HOUR_IN_MILLIS = 3600 * 1000; - private static final long MINUTE_IN_MILLIS = 60 * 1000; - private static final long SECOND_IN_MILLIS = 1000; - - /** - * 鎸囧畾妯″紡鐨勬椂闂存牸寮� - * @param pattern - * @return - */ - private static SimpleDateFormat getSdFormat(String pattern) { - return new SimpleDateFormat(pattern); - } - - /** - * 褰撳墠鏃ュ巻锛岃繖閲岀敤涓浗鏃堕棿琛ㄧず - * - * @return 浠ュ綋鍦版椂鍖鸿〃绀虹殑绯荤粺褰撳墠鏃ュ巻 - */ - public static Calendar getCalendar() { - return Calendar.getInstance(); - } - - /** - * 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻 - * - * @param millis 姣鏁� - * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻 - */ - public static Calendar getCalendar(long millis) { - Calendar cal = Calendar.getInstance(); - // --------------------cal.setTimeInMillis(millis); - cal.setTime(new Date(millis)); - return cal; - } - - // //////////////////////////////////////////////////////////////////////////// - // getDate - // 鍚勭鏂瑰紡鑾峰彇鐨凞ate - // //////////////////////////////////////////////////////////////////////////// - - /** - * 褰撳墠鏃ユ湡 - * - * @return 绯荤粺褰撳墠鏃堕棿 - */ - public static Date getDate() { - return new Date(); - } - - /** - * 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡 - * - * @param millis 姣鏁� - * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡 - */ - public static Date getDate(long millis) { - return new Date(millis); - } - - /** - * 鏃堕棿鎴宠浆鎹负瀛楃涓� - * - * @param time - * @return - */ - public static String timestamptoStr(Timestamp time) { - Date date = null; - if (null != time) { - date = new Date(time.getTime()); - } - return date2Str(date_sdf.get()); - } - - /** - * 瀛楃涓茶浆鎹㈡椂闂存埑 - * - * @param str - * @return - */ - public static Timestamp str2Timestamp(String str) { - Date date = str2Date(str, date_sdf.get()); - return new Timestamp(date.getTime()); - } - - /** - * 瀛楃涓茶浆鎹㈡垚鏃ユ湡 - * - * @param str - * @param sdf - * @return - */ - public static Date str2Date(String str, SimpleDateFormat sdf) { - if (null == str || "".equals(str)) { - return null; - } - Date date = null; - try { - date = sdf.parse(str); - return date; - } catch (ParseException e) { - e.printStackTrace(); - } - return null; - } - - /** - * 鏃ユ湡杞崲涓哄瓧绗︿覆 - * - * @param dateSdf 鏃ユ湡鏍煎紡 - * @return 瀛楃涓� - */ - public static String date2Str(SimpleDateFormat dateSdf) { - synchronized (dateSdf) { - Date date = getDate(); - if (null == date) { - return null; - } - return dateSdf.format(date); - } - } - - /** - * 鏍煎紡鍖栨椂闂� - * - * @param date - * @param format - * @return - */ - public static String dateformat(String date, String format) { - SimpleDateFormat sformat = new SimpleDateFormat(format); - Date nowDate = null; - try { - nowDate = sformat.parse(date); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return sformat.format(nowDate); - } - - /** - * 鏃ユ湡杞崲涓哄瓧绗︿覆 - * - * @param date 鏃ユ湡 - * @param dateSdf 鏃ユ湡鏍煎紡 - * @return 瀛楃涓� - */ - public static String date2Str(Date date, SimpleDateFormat dateSdf) { - synchronized (dateSdf) { - if (null == date) { - return null; - } - return dateSdf.format(date); - } - } - - /** - * 鏃ユ湡杞崲涓哄瓧绗︿覆 - * - * @param format 鏃ユ湡鏍煎紡 - * @return 瀛楃涓� - */ - public static String getDate(String format) { - Date date = new Date(); - if (null == date) { - return null; - } - SimpleDateFormat sdf = new SimpleDateFormat(format); - return sdf.format(date); - } - - /** - * 鎸囧畾姣鏁扮殑鏃堕棿鎴� - * - * @param millis 姣鏁� - * @return 鎸囧畾姣鏁扮殑鏃堕棿鎴� - */ - public static Timestamp getTimestamp(long millis) { - return new Timestamp(millis); - } - - /** - * 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴� - * - * @param time 姣鏁� - * @return 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴� - */ - public static Timestamp getTimestamp(String time) { - return new Timestamp(Long.parseLong(time)); - } - - /** - * 绯荤粺褰撳墠鐨勬椂闂存埑 - * - * @return 绯荤粺褰撳墠鐨勬椂闂存埑 - */ - public static Timestamp getTimestamp() { - return new Timestamp(System.currentTimeMillis()); - } - - /** - * 褰撳墠鏃堕棿锛屾牸寮� yyyy-MM-dd HH:mm:ss - * - * @return 褰撳墠鏃堕棿鐨勬爣鍑嗗舰寮忓瓧绗︿覆 - */ - public static String now() { - return datetimeFormat.get().format(getCalendar().getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鐨勬椂闂存埑 - * - * @param date 鎸囧畾鏃ユ湡 - * @return 鎸囧畾鏃ユ湡鐨勬椂闂存埑 - */ - public static Timestamp getTimestamp(Date date) { - return new Timestamp(date.getTime()); - } - - /** - * 鎸囧畾鏃ュ巻鐨勬椂闂存埑 - * - * @param cal 鎸囧畾鏃ュ巻 - * @return 鎸囧畾鏃ュ巻鐨勬椂闂存埑 - */ - public static Timestamp getCalendarTimestamp(Calendar cal) { - // ---------------------return new Timestamp(cal.getTimeInMillis()); - return new Timestamp(cal.getTime().getTime()); - } - - public static Timestamp gettimestamp() { - Date dt = new Date(); - DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String nowTime = df.format(dt); - java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime); - return buydate; - } - - // //////////////////////////////////////////////////////////////////////////// - // getMillis - // 鍚勭鏂瑰紡鑾峰彇鐨凪illis - // //////////////////////////////////////////////////////////////////////////// - - /** - * 绯荤粺鏃堕棿鐨勬绉掓暟 - * - * @return 绯荤粺鏃堕棿鐨勬绉掓暟 - */ - public static long getMillis() { - return System.currentTimeMillis(); - } - - /** - * 鎸囧畾鏃ュ巻鐨勬绉掓暟 - * - * @param cal 鎸囧畾鏃ュ巻 - * @return 鎸囧畾鏃ュ巻鐨勬绉掓暟 - */ - public static long getMillis(Calendar cal) { - // --------------------return cal.getTimeInMillis(); - return cal.getTime().getTime(); - } - - /** - * 鎸囧畾鏃ユ湡鐨勬绉掓暟 - * - * @param date 鎸囧畾鏃ユ湡 - * @return 鎸囧畾鏃ユ湡鐨勬绉掓暟 - */ - public static long getMillis(Date date) { - return date.getTime(); - } - - /** - * 鎸囧畾鏃堕棿鎴崇殑姣鏁� - * - * @param ts 鎸囧畾鏃堕棿鎴� - * @return 鎸囧畾鏃堕棿鎴崇殑姣鏁� - */ - public static long getMillis(Timestamp ts) { - return ts.getTime(); - } - - // //////////////////////////////////////////////////////////////////////////// - // formatDate - // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 - // //////////////////////////////////////////////////////////////////////////// - - /** - * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� - * - * @return 榛樿鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� - */ - public static String formatDate() { - return date_sdf.get().format(getCalendar().getTime()); - } - - /** - * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛歽yyy-MM-dd HH:mm:ss - * - * @return 榛樿鏃ユ湡鎸夆�測yyy-MM-dd HH:mm:ss鈥滄牸寮忔樉绀� - */ - public static String formatDateTime() { - return datetimeFormat.get().format(getCalendar().getTime()); - } - - /** - * 鑾峰彇鏃堕棿瀛楃涓� - */ - public static String getDataString(SimpleDateFormat formatstr) { - synchronized (formatstr) { - return formatstr.format(getCalendar().getTime()); - } - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� - * - * @param cal 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� - */ - public static String formatDate(Calendar cal) { - return date_sdf.get().format(cal.getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� - * - * @param date 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� - */ - public static String formatDate(Date date) { - return date_sdf.get().format(date); - } - - /** - * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�-鏈�-鏃� - * - * @param millis 鎸囧畾鐨勬绉掓暟 - * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�-鏃モ�滄牸寮忔樉绀� - */ - public static String formatDate(long millis) { - return date_sdf.get().format(new Date(millis)); - } - - /** - * 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - * - * @param pattern 鎸囧畾鐨勬牸寮� - * @return 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - */ - public static String formatDate(String pattern) { - return getSdFormat(pattern).format(getCalendar().getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - * - * @param cal 鎸囧畾鐨勬棩鏈� - * @param pattern 鎸囧畾鐨勬牸寮� - * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - */ - public static String formatDate(Calendar cal, String pattern) { - return getSdFormat(pattern).format(cal.getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - * - * @param date 鎸囧畾鐨勬棩鏈� - * @param pattern 鎸囧畾鐨勬牸寮� - * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� - */ - public static String formatDate(Date date, String pattern) { - return getSdFormat(pattern).format(date); - } - - // //////////////////////////////////////////////////////////////////////////// - // formatTime - // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 - // //////////////////////////////////////////////////////////////////////////// - - /** - * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� - * - * @return 榛樿鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� - */ - public static String formatTime() { - return time_sdf.get().format(getCalendar().getTime()); - } - - /** - * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�-鏈�-鏃� 鏃讹細鍒� - * - * @param millis 鎸囧畾鐨勬绉掓暟 - * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� - */ - public static String formatTime(long millis) { - return time_sdf.get().format(new Date(millis)); - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� - * - * @param cal 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� - */ - public static String formatTime(Calendar cal) { - return time_sdf.get().format(cal.getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� - * - * @param date 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� - */ - public static String formatTime(Date date) { - return time_sdf.get().format(date); - } - - // //////////////////////////////////////////////////////////////////////////// - // formatShortTime - // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 - // //////////////////////////////////////////////////////////////////////////// - - /** - * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 - * - * @return 榛樿鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� - */ - public static String formatShortTime() { - return short_time_sdf.get().format(getCalendar().getTime()); - } - - /** - * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細鏃讹細鍒� - * - * @param millis 鎸囧畾鐨勬绉掓暟 - * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滄椂锛氬垎鈥滄牸寮忔樉绀� - */ - public static String formatShortTime(long millis) { - return short_time_sdf.get().format(new Date(millis)); - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 - * - * @param cal 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� - */ - public static String formatShortTime(Calendar cal) { - return short_time_sdf.get().format(cal.getTime()); - } - - /** - * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 - * - * @param date 鎸囧畾鐨勬棩鏈� - * @return 鎸囧畾鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� - */ - public static String formatShortTime(Date date) { - return short_time_sdf.get().format(date); - } - - // //////////////////////////////////////////////////////////////////////////// - // parseDate - // parseCalendar - // parseTimestamp - // 灏嗗瓧绗︿覆鎸夌収涓�瀹氱殑鏍煎紡杞寲涓烘棩鏈熸垨鏃堕棿 - // //////////////////////////////////////////////////////////////////////////// - - /** - * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� - * - * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 - * @param pattern 杞崲鐨勫尮閰嶆牸寮� - * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈� - * @throws ParseException - */ - public static Date parseDate(String src, String pattern) throws ParseException { - return getSdFormat(pattern).parse(src); - - } - - /** - * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� - * - * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 - * @param pattern 杞崲鐨勫尮閰嶆牸寮� - * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈� - * @throws ParseException - */ - public static Calendar parseCalendar(String src, String pattern) throws ParseException { - - Date date = parseDate(src, pattern); - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - return cal; - } - - public static String formatAddDate(String src, String pattern, int amount) throws ParseException { - Calendar cal; - cal = parseCalendar(src, pattern); - cal.add(Calendar.DATE, amount); - return formatDate(cal); - } - - /** - * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� - * - * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 - * @param pattern 杞崲鐨勫尮閰嶆牸寮� - * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬椂闂存埑 - * @throws ParseException - */ - public static Timestamp parseTimestamp(String src, String pattern) throws ParseException { - Date date = parseDate(src, pattern); - return new Timestamp(date.getTime()); - } - - // //////////////////////////////////////////////////////////////////////////// - // dateDiff - // 璁$畻涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊� - // //////////////////////////////////////////////////////////////////////////// - - /** - * 璁$畻涓や釜鏃堕棿涔嬮棿鐨勫樊鍊硷紝鏍规嵁鏍囧織鐨勪笉鍚岃�屼笉鍚� - * - * @param flag 璁$畻鏍囧織锛岃〃绀烘寜鐓у勾/鏈�/鏃�/鏃�/鍒�/绉掔瓑璁$畻 - * @param calSrc 鍑忔暟 - * @param calDes 琚噺鏁� - * @return 涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊� - */ - public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) { - - long millisDiff = getMillis(calSrc) - getMillis(calDes); - char year = 'y'; - char day = 'd'; - char hour = 'h'; - char minute = 'm'; - char second = 's'; - - if (flag == year) { - return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR)); - } - - if (flag == day) { - return (int) (millisDiff / DAY_IN_MILLIS); - } - - if (flag == hour) { - return (int) (millisDiff / HOUR_IN_MILLIS); - } - - if (flag == minute) { - return (int) (millisDiff / MINUTE_IN_MILLIS); - } - - if (flag == second) { - return (int) (millisDiff / SECOND_IN_MILLIS); - } - - return 0; - } - - public static Long getCurrentTimestamp() { - return Long.valueOf(DateUtils.yyyymmddhhmmss.get().format(new Date())); - } - - /** - * String绫诲瀷 杞崲涓篋ate, 濡傛灉鍙傛暟闀垮害涓�10 杞崲鏍煎紡鈥漼yyy-MM-dd鈥� 濡傛灉鍙傛暟闀垮害涓�19 杞崲鏍煎紡鈥漼yyy-MM-dd - * HH:mm:ss鈥� * @param text String绫诲瀷鐨勬椂闂村�� - */ + public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() { @Override - public void setAsText(String text) throws IllegalArgumentException { - if (StringUtils.hasText(text)) { - try { - int length10 = 10; - int length19 = 19; - if (text.indexOf(SymbolConstant.COLON) == -1 && text.length() == length10) { - setValue(DateUtils.date_sdf.get().parse(text)); - } else if (text.indexOf(SymbolConstant.COLON) > 0 && text.length() == length19) { - setValue(DateUtils.datetimeFormat.get().parse(text)); - } else { - throw new IllegalArgumentException("Could not parse date, date format is error "); - } - } catch (ParseException ex) { - IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage()); - iae.initCause(ex); - throw iae; - } - } else { - setValue(null); - } + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM-dd"); } - - public static int getYear() { - GregorianCalendar calendar = new GregorianCalendar(); - calendar.setTime(getDate()); - return calendar.get(Calendar.YEAR); + }; + public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyyMMdd"); } + }; + public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy骞碝M鏈坉d鏃�"); + } + }; + public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM-dd HH:mm"); + } + }; + public static ThreadLocal<SimpleDateFormat> yyyymmddhhmmss = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyyMMddHHmmss"); + } + }; + public static ThreadLocal<SimpleDateFormat> short_time_sdf = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("HH:mm"); + } + }; + public static ThreadLocal<SimpleDateFormat> datetimeFormat = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + } + }; - /** - * 灏嗗瓧绗︿覆杞垚鏃堕棿 - * @param str - * @return - */ - public static Date parseDatetime(String str){ - try { - return datetimeFormat.get().parse(str); - }catch (Exception e){ - } + /** + * 浠ユ绉掕〃绀虹殑鏃堕棿 + */ + private static final long DAY_IN_MILLIS = 24 * 3600 * 1000; + private static final long HOUR_IN_MILLIS = 3600 * 1000; + private static final long MINUTE_IN_MILLIS = 60 * 1000; + private static final long SECOND_IN_MILLIS = 1000; + + /** + * 鎸囧畾妯″紡鐨勬椂闂存牸寮� + * + * @param pattern + * @return + */ + private static SimpleDateFormat getSdFormat(String pattern) { + return new SimpleDateFormat(pattern); + } + + /** + * 褰撳墠鏃ュ巻锛岃繖閲岀敤涓浗鏃堕棿琛ㄧず + * + * @return 浠ュ綋鍦版椂鍖鸿〃绀虹殑绯荤粺褰撳墠鏃ュ巻 + */ + public static Calendar getCalendar() { + return Calendar.getInstance(); + } + + /** + * 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻 + * + * @param millis 姣鏁� + * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ュ巻 + */ + public static Calendar getCalendar(long millis) { + Calendar cal = Calendar.getInstance(); + // --------------------cal.setTimeInMillis(millis); + cal.setTime(new Date(millis)); + return cal; + } + + // //////////////////////////////////////////////////////////////////////////// + // getDate + // 鍚勭鏂瑰紡鑾峰彇鐨凞ate + // //////////////////////////////////////////////////////////////////////////// + + /** + * 褰撳墠鏃ユ湡 + * + * @return 绯荤粺褰撳墠鏃堕棿 + */ + public static Date getDate() { + return new Date(); + } + + /** + * 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡 + * + * @param millis 姣鏁� + * @return 鎸囧畾姣鏁拌〃绀虹殑鏃ユ湡 + */ + public static Date getDate(long millis) { + return new Date(millis); + } + + /** + * 鏃堕棿鎴宠浆鎹负瀛楃涓� + * + * @param time + * @return + */ + public static String timestamptoStr(Timestamp time) { + Date date = null; + if (null != time) { + date = new Date(time.getTime()); + } + return date2Str(date_sdf.get()); + } + /** + * 鏃堕棿鎴宠浆鎹负瀛楃涓� + * + * @param time + * @return + */ + public static String timestamptoStr(Long time, SimpleDateFormat sdf) { + Date date = null; + if (null != time) { + date = new Date(time); + } + return date2Str(date, sdf); + } + + + /** + * 瀛楃涓茶浆鎹㈡椂闂存埑 + * + * @param str + * @return + */ + public static Timestamp str2Timestamp(String str) { + Date date = str2Date(str, date_sdf.get()); + return new Timestamp(date.getTime()); + } + + /** + * 瀛楃涓茶浆鎹㈡垚鏃ユ湡 + * + * @param str + * @param sdf + * @return + */ + public static Date str2Date(String str, SimpleDateFormat sdf) { + if (null == str || "".equals(str)) { + return null; + } + Date date = null; + try { + date = sdf.parse(str); + return date; + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 鏃ユ湡杞崲涓哄瓧绗︿覆 + * + * @param dateSdf 鏃ユ湡鏍煎紡 + * @return 瀛楃涓� + */ + public static String date2Str(SimpleDateFormat dateSdf) { + synchronized (dateSdf) { + Date date = getDate(); + if (null == date) { return null; + } + return dateSdf.format(date); + } + } + + /** + * 鏍煎紡鍖栨椂闂� + * + * @param date + * @param format + * @return + */ + public static String dateformat(String date, String format) { + SimpleDateFormat sformat = new SimpleDateFormat(format); + Date nowDate = null; + try { + nowDate = sformat.parse(date); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return sformat.format(nowDate); + } + + /** + * 鏃ユ湡杞崲涓哄瓧绗︿覆 + * + * @param date 鏃ユ湡 + * @param dateSdf 鏃ユ湡鏍煎紡 + * @return 瀛楃涓� + */ + public static String date2Str(Date date, SimpleDateFormat dateSdf) { + synchronized (dateSdf) { + if (null == date) { + return null; + } + return dateSdf.format(date); + } + } + + /** + * 鏃ユ湡杞崲涓哄瓧绗︿覆 + * + * @param format 鏃ユ湡鏍煎紡 + * @return 瀛楃涓� + */ + public static String getDate(String format) { + Date date = new Date(); + if (null == date) { + return null; + } + SimpleDateFormat sdf = new SimpleDateFormat(format); + return sdf.format(date); + } + + /** + * 鎸囧畾姣鏁扮殑鏃堕棿鎴� + * + * @param millis 姣鏁� + * @return 鎸囧畾姣鏁扮殑鏃堕棿鎴� + */ + public static Timestamp getTimestamp(long millis) { + return new Timestamp(millis); + } + + /** + * 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴� + * + * @param time 姣鏁� + * @return 浠ュ瓧绗﹀舰寮忚〃绀虹殑鏃堕棿鎴� + */ + public static Timestamp getTimestamp(String time) { + return new Timestamp(Long.parseLong(time)); + } + + /** + * 绯荤粺褰撳墠鐨勬椂闂存埑 + * + * @return 绯荤粺褰撳墠鐨勬椂闂存埑 + */ + public static Timestamp getTimestamp() { + return new Timestamp(System.currentTimeMillis()); + } + + /** + * 褰撳墠鏃堕棿锛屾牸寮� yyyy-MM-dd HH:mm:ss + * + * @return 褰撳墠鏃堕棿鐨勬爣鍑嗗舰寮忓瓧绗︿覆 + */ + public static String now() { + return datetimeFormat.get().format(getCalendar().getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鐨勬椂闂存埑 + * + * @param date 鎸囧畾鏃ユ湡 + * @return 鎸囧畾鏃ユ湡鐨勬椂闂存埑 + */ + public static Timestamp getTimestamp(Date date) { + return new Timestamp(date.getTime()); + } + + /** + * 鎸囧畾鏃ュ巻鐨勬椂闂存埑 + * + * @param cal 鎸囧畾鏃ュ巻 + * @return 鎸囧畾鏃ュ巻鐨勬椂闂存埑 + */ + public static Timestamp getCalendarTimestamp(Calendar cal) { + // ---------------------return new Timestamp(cal.getTimeInMillis()); + return new Timestamp(cal.getTime().getTime()); + } + + public static Timestamp gettimestamp() { + Date dt = new Date(); + DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String nowTime = df.format(dt); + java.sql.Timestamp buydate = java.sql.Timestamp.valueOf(nowTime); + return buydate; + } + + // //////////////////////////////////////////////////////////////////////////// + // getMillis + // 鍚勭鏂瑰紡鑾峰彇鐨凪illis + // //////////////////////////////////////////////////////////////////////////// + + /** + * 绯荤粺鏃堕棿鐨勬绉掓暟 + * + * @return 绯荤粺鏃堕棿鐨勬绉掓暟 + */ + public static long getMillis() { + return System.currentTimeMillis(); + } + + /** + * 鎸囧畾鏃ュ巻鐨勬绉掓暟 + * + * @param cal 鎸囧畾鏃ュ巻 + * @return 鎸囧畾鏃ュ巻鐨勬绉掓暟 + */ + public static long getMillis(Calendar cal) { + // --------------------return cal.getTimeInMillis(); + return cal.getTime().getTime(); + } + + /** + * 鎸囧畾鏃ユ湡鐨勬绉掓暟 + * + * @param date 鎸囧畾鏃ユ湡 + * @return 鎸囧畾鏃ユ湡鐨勬绉掓暟 + */ + public static long getMillis(Date date) { + return date.getTime(); + } + + /** + * 鎸囧畾鏃堕棿鎴崇殑姣鏁� + * + * @param ts 鎸囧畾鏃堕棿鎴� + * @return 鎸囧畾鏃堕棿鎴崇殑姣鏁� + */ + public static long getMillis(Timestamp ts) { + return ts.getTime(); + } + + // //////////////////////////////////////////////////////////////////////////// + // formatDate + // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 + // //////////////////////////////////////////////////////////////////////////// + + /** + * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� + * + * @return 榛樿鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� + */ + public static String formatDate() { + return date_sdf.get().format(getCalendar().getTime()); + } + + /** + * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛歽yyy-MM-dd HH:mm:ss + * + * @return 榛樿鏃ユ湡鎸夆�測yyy-MM-dd HH:mm:ss鈥滄牸寮忔樉绀� + */ + public static String formatDateTime() { + return datetimeFormat.get().format(getCalendar().getTime()); + } + + /** + * 鑾峰彇鏃堕棿瀛楃涓� + */ + public static String getDataString(SimpleDateFormat formatstr) { + synchronized (formatstr) { + return formatstr.format(getCalendar().getTime()); + } + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� + * + * @param cal 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� + */ + public static String formatDate(Calendar cal) { + return date_sdf.get().format(cal.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� + * + * @param date 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃モ�滄牸寮忔樉绀� + */ + public static String formatDate(Date date) { + return date_sdf.get().format(date); + } + + /** + * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�-鏈�-鏃� + * + * @param millis 鎸囧畾鐨勬绉掓暟 + * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�-鏃モ�滄牸寮忔樉绀� + */ + public static String formatDate(long millis) { + return date_sdf.get().format(new Date(millis)); + } + + /** + * 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + * + * @param pattern 鎸囧畾鐨勬牸寮� + * @return 榛樿鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + */ + public static String formatDate(String pattern) { + return getSdFormat(pattern).format(getCalendar().getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + * + * @param cal 鎸囧畾鐨勬棩鏈� + * @param pattern 鎸囧畾鐨勬牸寮� + * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + */ + public static String formatDate(Calendar cal, String pattern) { + return getSdFormat(pattern).format(cal.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + * + * @param date 鎸囧畾鐨勬棩鏈� + * @param pattern 鎸囧畾鐨勬牸寮� + * @return 鎸囧畾鏃ユ湡鎸夋寚瀹氭牸寮忔樉绀� + */ + public static String formatDate(Date date, String pattern) { + return getSdFormat(pattern).format(date); + } + + // //////////////////////////////////////////////////////////////////////////// + // formatTime + // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 + // //////////////////////////////////////////////////////////////////////////// + + /** + * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� + * + * @return 榛樿鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� + */ + public static String formatTime() { + return time_sdf.get().format(getCalendar().getTime()); + } + + /** + * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細骞�-鏈�-鏃� 鏃讹細鍒� + * + * @param millis 鎸囧畾鐨勬绉掓暟 + * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� + */ + public static String formatTime(long millis) { + return time_sdf.get().format(new Date(millis)); + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� + * + * @param cal 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� + */ + public static String formatTime(Calendar cal) { + return time_sdf.get().format(cal.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氬勾-鏈�-鏃� 鏃讹細鍒� + * + * @param date 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滃勾-鏈�-鏃� 鏃讹細鍒嗏�滄牸寮忔樉绀� + */ + public static String formatTime(Date date) { + return time_sdf.get().format(date); + } + + // //////////////////////////////////////////////////////////////////////////// + // formatShortTime + // 灏嗘棩鏈熸寜鐓т竴瀹氱殑鏍煎紡杞寲涓哄瓧绗︿覆 + // //////////////////////////////////////////////////////////////////////////// + + /** + * 榛樿鏂瑰紡琛ㄧず鐨勭郴缁熷綋鍓嶆棩鏈燂紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 + * + * @return 榛樿鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� + */ + public static String formatShortTime() { + return short_time_sdf.get().format(getCalendar().getTime()); + } + + /** + * 鎸囧畾姣鏁拌〃绀烘棩鏈熺殑榛樿鏄剧ず锛屽叿浣撴牸寮忥細鏃讹細鍒� + * + * @param millis 鎸囧畾鐨勬绉掓暟 + * @return 鎸囧畾姣鏁拌〃绀烘棩鏈熸寜鈥滄椂锛氬垎鈥滄牸寮忔樉绀� + */ + public static String formatShortTime(long millis) { + return short_time_sdf.get().format(new Date(millis)); + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 + * + * @param cal 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� + */ + public static String formatShortTime(Calendar cal) { + return short_time_sdf.get().format(cal.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鐨勯粯璁ゆ樉绀猴紝鍏蜂綋鏍煎紡锛氭椂锛氬垎 + * + * @param date 鎸囧畾鐨勬棩鏈� + * @return 鎸囧畾鏃ユ湡鎸夆�滄椂锛氬垎鈥滄牸寮忔樉绀� + */ + public static String formatShortTime(Date date) { + return short_time_sdf.get().format(date); + } + + // //////////////////////////////////////////////////////////////////////////// + // parseDate + // parseCalendar + // parseTimestamp + // 灏嗗瓧绗︿覆鎸夌収涓�瀹氱殑鏍煎紡杞寲涓烘棩鏈熸垨鏃堕棿 + // //////////////////////////////////////////////////////////////////////////// + + /** + * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� + * + * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 + * @param pattern 杞崲鐨勫尮閰嶆牸寮� + * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈� + * @throws ParseException + */ + public static Date parseDate(String src, String pattern) throws ParseException { + return getSdFormat(pattern).parse(src); + + } + + /** + * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� + * + * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 + * @param pattern 杞崲鐨勫尮閰嶆牸寮� + * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬棩鏈� + * @throws ParseException + */ + public static Calendar parseCalendar(String src, String pattern) throws ParseException { + + Date date = parseDate(src, pattern); + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + return cal; + } + + public static String formatAddDate(String src, String pattern, int amount) throws ParseException { + Calendar cal; + cal = parseCalendar(src, pattern); + cal.add(Calendar.DATE, amount); + return formatDate(cal); + } + + /** + * 鏍规嵁鎸囧畾鐨勬牸寮忓皢瀛楃涓茶浆鎹㈡垚Date 濡傝緭鍏ワ細2003-11-19 11:20:20灏嗘寜鐓ц繖涓浆鎴愭椂闂� + * + * @param src 灏嗚杞崲鐨勫師濮嬪瓧绗︾獪 + * @param pattern 杞崲鐨勫尮閰嶆牸寮� + * @return 濡傛灉杞崲鎴愬姛鍒欒繑鍥炶浆鎹㈠悗鐨勬椂闂存埑 + * @throws ParseException + */ + public static Timestamp parseTimestamp(String src, String pattern) throws ParseException { + Date date = parseDate(src, pattern); + return new Timestamp(date.getTime()); + } + + // //////////////////////////////////////////////////////////////////////////// + // dateDiff + // 璁$畻涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊� + // //////////////////////////////////////////////////////////////////////////// + + /** + * 璁$畻涓や釜鏃堕棿涔嬮棿鐨勫樊鍊硷紝鏍规嵁鏍囧織鐨勪笉鍚岃�屼笉鍚� + * + * @param flag 璁$畻鏍囧織锛岃〃绀烘寜鐓у勾/鏈�/鏃�/鏃�/鍒�/绉掔瓑璁$畻 + * @param calSrc 鍑忔暟 + * @param calDes 琚噺鏁� + * @return 涓や釜鏃ユ湡涔嬮棿鐨勫樊鍊� + */ + public static int dateDiff(char flag, Calendar calSrc, Calendar calDes) { + + long millisDiff = getMillis(calSrc) - getMillis(calDes); + char year = 'y'; + char day = 'd'; + char hour = 'h'; + char minute = 'm'; + char second = 's'; + + if (flag == year) { + return (calSrc.get(Calendar.YEAR) - calDes.get(Calendar.YEAR)); } - /** - * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�澶� - * - * @param date1 - * @param date2 - * @return - */ - public static boolean isSameDay(Date date1, Date date2) { - if (date1 == null || date2 == null) { - return false; + if (flag == day) { + return (int) (millisDiff / DAY_IN_MILLIS); + } + + if (flag == hour) { + return (int) (millisDiff / HOUR_IN_MILLIS); + } + + if (flag == minute) { + return (int) (millisDiff / MINUTE_IN_MILLIS); + } + + if (flag == second) { + return (int) (millisDiff / SECOND_IN_MILLIS); + } + + return 0; + } + + public static Long getCurrentTimestamp() { + return Long.valueOf(DateUtils.yyyymmddhhmmss.get().format(new Date())); + } + + /** + * String绫诲瀷 杞崲涓篋ate, 濡傛灉鍙傛暟闀垮害涓�10 杞崲鏍煎紡鈥漼yyy-MM-dd鈥� 濡傛灉鍙傛暟闀垮害涓�19 杞崲鏍煎紡鈥漼yyy-MM-dd + * HH:mm:ss鈥� * @param text String绫诲瀷鐨勬椂闂村�� + */ + @Override + public void setAsText(String text) throws IllegalArgumentException { + if (StringUtils.hasText(text)) { + try { + int length10 = 10; + int length19 = 19; + if (text.indexOf(SymbolConstant.COLON) == -1 && text.length() == length10) { + setValue(DateUtils.date_sdf.get().parse(text)); + } else if (text.indexOf(SymbolConstant.COLON) > 0 && text.length() == length19) { + setValue(DateUtils.datetimeFormat.get().parse(text)); + } else { + throw new IllegalArgumentException("Could not parse date, date format is error "); } - Calendar calendar1 = Calendar.getInstance(); - calendar1.setTime(date1); - Calendar calendar2 = Calendar.getInstance(); - calendar2.setTime(date2); - boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); - boolean isSameMonth = isSameYear && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH); - return isSameMonth && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH); + } catch (ParseException ex) { + IllegalArgumentException iae = new IllegalArgumentException("Could not parse date: " + ex.getMessage()); + iae.initCause(ex); + throw iae; + } + } else { + setValue(null); } + } - /** - * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�鍛� - * - * @param date1 - * @param date2 - * @return - */ - public static boolean isSameWeek(Date date1, Date date2) { - if (date1 == null || date2 == null) { - return false; - } - Calendar calendar1 = Calendar.getInstance(); - calendar1.setTime(date1); - Calendar calendar2 = Calendar.getInstance(); - calendar2.setTime(date2); - boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); - return isSameYear && calendar1.get(Calendar.WEEK_OF_YEAR) == calendar2.get(Calendar.WEEK_OF_YEAR); + public static int getYear() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTime(getDate()); + return calendar.get(Calendar.YEAR); + } + + /** + * 灏嗗瓧绗︿覆杞垚鏃堕棿 + * + * @param str + * @return + */ + public static Date parseDatetime(String str) { + try { + return datetimeFormat.get().parse(str); + } catch (Exception e) { } + return null; + } - /** - * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�鏈� - * - * @param date1 - * @param date2 - * @return - */ - public static boolean isSameMonth(Date date1, Date date2) { - if (date1 == null || date2 == null) { - return false; - } - Calendar calendar1 = Calendar.getInstance(); - calendar1.setTime(date1); - Calendar calendar2 = Calendar.getInstance(); - calendar2.setTime(date2); - boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); - return isSameYear && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH); + /** + * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�澶� + * + * @param date1 + * @param date2 + * @return + */ + public static boolean isSameDay(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return false; } + Calendar calendar1 = Calendar.getInstance(); + calendar1.setTime(date1); + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTime(date2); + boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); + boolean isSameMonth = isSameYear && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH); + return isSameMonth && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH); + } - /** - * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�骞� - * - * @param date1 - * @param date2 - * @return - */ - public static boolean isSameYear(Date date1, Date date2) { - if (date1 == null || date2 == null) { - return false; - } - Calendar calendar1 = Calendar.getInstance(); - calendar1.setTime(date1); - Calendar calendar2 = Calendar.getInstance(); - calendar2.setTime(date2); - return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); + /** + * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�鍛� + * + * @param date1 + * @param date2 + * @return + */ + public static boolean isSameWeek(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return false; } + Calendar calendar1 = Calendar.getInstance(); + calendar1.setTime(date1); + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTime(date2); + boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); + return isSameYear && calendar1.get(Calendar.WEEK_OF_YEAR) == calendar2.get(Calendar.WEEK_OF_YEAR); + } -} \ No newline at end of file + /** + * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�鏈� + * + * @param date1 + * @param date2 + * @return + */ + public static boolean isSameMonth(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return false; + } + Calendar calendar1 = Calendar.getInstance(); + calendar1.setTime(date1); + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTime(date2); + boolean isSameYear = calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); + return isSameYear && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH); + } + + /** + * 鐗规畩鏃堕棿str杞甯竤tr + * + * @param st 2024-08-14T01:07:36.761+00:00 + * @return + */ + public static String zone2Str(String st) { + // 瑙f瀽瀛楃涓蹭负ZonedDateTime瀵硅薄 + ZonedDateTime zonedDateTime = ZonedDateTime.parse(st); + // 杞崲涓轰腑鍥芥爣鍑嗘椂闂达紙UTC+8锛� + ZonedDateTime cstDateTime = zonedDateTime.withZoneSameInstant(java.time.ZoneOffset.ofHours(8)); + // 瀹氫箟鎯宠鐨勮緭鍑烘牸寮� + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + // 鏍煎紡鍖栦负鎯宠鐨勫瓧绗︿覆 + String str = cstDateTime.format(formatter); + return str; + } + + /** + * 鍒ゆ柇涓や釜鏃堕棿鏄惁鏄悓涓�骞� + * + * @param date1 + * @param date2 + * @return + */ + public static boolean isSameYear(Date date1, Date date2) { + if (date1 == null || date2 == null) { + return false; + } + Calendar calendar1 = Calendar.getInstance(); + calendar1.setTime(date1); + Calendar calendar2 = Calendar.getInstance(); + calendar2.setTime(date2); + return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR); + } + +} -- Gitblit v1.9.3