| | |
| | | public DateUtil() { |
| | | } |
| | | |
| | | /** |
| | | * 传入Data类型日期,返回字符串类型时间(ISO8601标准时间) |
| | | * @param date |
| | | * @return |
| | | */ |
| | | public static String getISO8601Timestamp(Date date){ |
| | | try { |
| | | TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | //TimeZone tz = TimeZone.getTimeZone("GMT-01"); |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+'08:00"); |
| | | df.setTimeZone(tz); |
| | | String nowAsISO = df.format(date); |
| | | return nowAsISO; |
| | | }catch (Exception e){ |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 传入Data类型日期,返回字符串类型时间(ISO8601标准时间) |
| | | * @param date |
| | | * @return |
| | | */ |
| | | public static String getISO8601Timestamp2(Date date){ |
| | | try { |
| | | TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | //TimeZone tz = TimeZone.getTimeZone("GMT-01"); |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'08:00"); |
| | | df.setTimeZone(tz); |
| | | String nowAsISO = df.format(date); |
| | | return nowAsISO; |
| | | }catch (Exception e){ |
| | | } |
| | | return null; |
| | | } |
| | | public static Date getISO8601DateByStr(String date) { |
| | | TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | if(date!=null ){ |
| | | int i = date.indexOf("+"); |
| | | if(i >0){ |
| | | date = date.substring(0,i); |
| | | } |
| | | } |
| | | //TimeZone tz = TimeZone.getTimeZone("GMT-01"); |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); |
| | | // DateFormat df = new SimpleDateFormat("2023-12-08 T 09:25:54.698+08:00"); |
| | | df.setTimeZone(tz); |
| | | Date nowAsISO = null; |
| | | try { |
| | | nowAsISO = df.parse(date); |
| | | } catch (ParseException e) { |
| | | } |
| | | return nowAsISO; |
| | | } |
| | | public static Date getISO8601DateByStr2(String date) { |
| | | TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | if(date!=null ){ |
| | | int i = date.indexOf("+"); |
| | | if(i >0){ |
| | | date = date.substring(0,i); |
| | | } |
| | | } |
| | | //TimeZone tz = TimeZone.getTimeZone("GMT-01"); |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
| | | // DateFormat df = new SimpleDateFormat("2023-12-08 T 09:25:54.698+08:00"); |
| | | df.setTimeZone(tz); |
| | | Date nowAsISO = null; |
| | | try { |
| | | nowAsISO = df.parse(date); |
| | | } catch (ParseException e) { |
| | | } |
| | | return nowAsISO; |
| | | } |
| | | |
| | | public static Date StringToDate2(String DATE) { |
| | | if(StringUtils.isBlank(DATE)){ |
| | | return null; |
| | |
| | | |
| | | if (date == null) |
| | | return ""; |
| | | try { |
| | | String nowDate = sdfLong.format(date); |
| | | return nowDate; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | public static String getShortTimeToNull(Date date) { |
| | | |
| | | if (date == null) |
| | | return null; |
| | | try { |
| | | String nowDate = sdfLong.format(date); |
| | | return nowDate; |
| | |
| | | public static void main(String[] args) { |
| | | try { |
| | | System.out.println(RandomStringUtils.randomNumeric(3)); |
| | | Date date = getISO8601DateByStr("2024-02-21T15:46:36.014+08:00"); |
| | | System.out.println(getISO8601Timestamp(date)); |
| | | |
| | | |
| | | |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | |
| | | } |
| | | return week; |
| | | } |
| | | |
| | | /** |
| | | * 时间转义带时区 入参格式 2022-09-13T00:53:03.000+00:00 |
| | | * @param timeEscape |
| | | * @return |
| | | * @throws ParseException |
| | | */ |
| | | public static String timeEscape(String timeEscape) throws ParseException { |
| | | SimpleDateFormat startDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX", Locale.US); |
| | | SimpleDateFormat startDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String time = startDateFormat1.format(startDateFormat.parse(timeEscape)); |
| | | System.out.println(time); |
| | | return time; |
| | | } |
| | | } |