From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期三, 25 六月 2025 11:51:38 +0800 Subject: [PATCH] Merge branch 'mdc_hyjs_master' --- lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java | 828 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 828 insertions(+), 0 deletions(-) diff --git a/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java new file mode 100644 index 0000000..f7ee46f --- /dev/null +++ b/lxzn-boot-base-core/src/main/java/org/jeecg/common/util/DateUtils.java @@ -0,0 +1,828 @@ +package org.jeecg.common.util; + +import java.beans.PropertyEditorSupport; +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import org.jeecg.common.constant.SymbolConstant; +import org.springframework.util.StringUtils; + +/** + * 绫绘弿杩帮細鏃堕棿鎿嶄綔瀹氫箟绫� + * + * @Author: 寮犱唬娴� + * @Date:2012-12-8 12:15:03 + * @Version 1.0 + */ +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"); + } + }; + public static ThreadLocal<SimpleDateFormat> hoursAndMinutes = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("HH:mm:ss"); + } + }; + public static ThreadLocal<SimpleDateFormat> monthFormat = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy-MM"); + } + }; + + public static ThreadLocal<SimpleDateFormat> yearFormat = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + return new SimpleDateFormat("yyyy"); + } + }; + + public static final String STR_DATE = "yyyy-MM-dd"; + public static final String STRDATE = "yyyyMMdd"; + public static final String STR_YEAR_MONTH = "yyyy-MM"; + public static final String STRYEARMONTH = "yyyyMM"; + public static final String STR_DATE_TIME = "yyyy-MM-dd HH:mm:ss.SSS"; + public static final String STR_DATE_TIME_SMALL = "yyyy-MM-dd HH:mm:ss"; + public static final String STR_DD_MM_YYYY = "dd/MM/yyyy HH:mm:ss"; + public static final String STR_DATE_TIME_MIN = "yyyy-MM-dd HH:mm"; + public static final String STR_DATE_TIME_HOUR = "yyyy-MM-dd HH"; + public static final String STR_DATE_TIME_FULL = "yyyyMMddHHmmssSSS"; + public static final String STR_HHMMSS = "HH:mm:ss"; + public static final String STR_HHMM = "HH:mm"; + + public static String format(Date date, String format) { + SimpleDateFormat df = new SimpleDateFormat(format); + return df.format(date); + } + + public static String formattedDate(String dateString, String dateformat, String format) { + // 瀹氫箟鍘熷鏃ユ湡瀛楃涓插拰鍏舵牸寮� + SimpleDateFormat originalFormat = new SimpleDateFormat(dateformat); + // 瀹氫箟鐩爣鏃ユ湡鏍煎紡 + SimpleDateFormat targetFormat = new SimpleDateFormat(format); + try { + // 瑙f瀽鍘熷鏃ユ湡瀛楃涓� + Date date = originalFormat.parse(dateString); + // 灏嗘棩鏈熸牸寮忓寲涓虹洰鏍囨牸寮� + String formattedDate = targetFormat.format(date); + + // 杈撳嚭杞崲鍚庣殑鏃ユ湡瀛楃涓� + System.out.println(formattedDate); + return formattedDate; + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + + + + /** + * 浠ユ绉掕〃绀虹殑鏃堕棿 + */ + 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绫诲瀷鐨勬椂闂村�� + */ + @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); + } + } + + public static int getYear() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTime(getDate()); + return calendar.get(Calendar.YEAR); + } + public static String getDayStr(Date date) { + Calendar calendar = getCalendar(); + calendar.setTime(date);// 鎶婂綋鍓嶆椂闂磋祴缁欐棩鍘� + int day = calendar.get(Calendar.DATE);// 鑾峰彇鏃� + String dayStr = day < 10 ? "0" + day : day + ""; + return dayStr; + } + /** + * 灏嗗瓧绗︿覆杞垚鏃堕棿 + * @param str + * @return + */ + public static Date parseDatetime(String str){ + try { + return datetimeFormat.get().parse(str); + }catch (Exception e){ + } + return null; + } + + /** + * 鑾峰彇鎸囧畾鏃堕棿涔嬪悗鐨勫嚑骞� qsw + */ + public static Date getYearAfter(Date data,int number) { + Calendar c = Calendar.getInstance(); + c.setTime(data); + c.add(Calendar.YEAR, number); + Date afterTime = c.getTime(); +// SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// String resultDate = f.format(afterTime); + return afterTime; + } + + /** + * 鑾峰彇鎸囧畾鏃堕棿涔嬪悗鐨勫嚑澶� qsw + */ + public static Date getDayAfter(Date data,int number) { + Calendar c = Calendar.getInstance(); + c.setTime(data); + c.add(Calendar.DAY_OF_MONTH, number); + Date afterTime = c.getTime(); +// SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// String resultDate = f.format(afterTime); + return afterTime; + } + + /** + * 鑾峰彇鎸囧畾鏃堕棿涔嬪悗鐨勫嚑鍒嗛挓 qsw + */ + public static Date getMinAfter(Date data,int number) { + Calendar c = Calendar.getInstance(); + c.setTime(data); + c.add(Calendar.MINUTE, number); + Date afterTime = c.getTime(); + return afterTime; + } + + /** + * 鑾峰彇鎸囧畾鏃堕棿涔嬪悗鐨勫嚑灏忔椂 qsw + */ + public static Date getHourAfter(Date data,int number) { + Calendar c = Calendar.getInstance(); + c.setTime(data); + c.add(Calendar.HOUR_OF_DAY, number); + Date afterTime = c.getTime(); + return afterTime; + } + + public static String getCurrentDateStr() { + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));// 璁剧疆鍖椾含鏃跺尯 + return dateFormat.format(date); + } + + /** + * Date 杞� LocalDate + * @param date + * @return + */ + public static LocalDate dateToLocalDate(Date date) { + return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + } + + /** + * LocalDate 杞� Date + * @param localDate + * @return + */ + public static Date localDateToDate(LocalDate localDate) { + return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + } +} \ No newline at end of file -- Gitblit v1.9.3