private static long parseIso8601(String value) { try { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(value).getTime(); } catch (ParseException ignored) { return 0; } }
private long parseDateHeader(String name, String value) { for (String dateFormat : DATE_FORMATS) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US); simpleDateFormat.setTimeZone(GMT); try { return simpleDateFormat.parse(value).getTime(); } catch (ParseException ex) { // ignore } } throw new IllegalArgumentException("Cannot parse date value '" + value + "' for '" + name + "' header"); }
public static Date parseDate(String date, String pattern) { SimpleDateFormat df = new SimpleDateFormat(pattern); try { return df.parse(date); } catch (ParseException e) { return null; } }
@Override public Object decode(Object jv) throws IOException { if (jv instanceof String) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) jv); } catch (ParseException e) { throw new IllegalArgumentException(e.getMessage(), e); } } if (jv instanceof Number) { return new Date(((Number) jv).longValue()); } return (Date) null; } };
public static Date toDate(Date date, String format) { SimpleDateFormat fmt = new SimpleDateFormat(format); String dateStr = fmt.format(date); Date outDate = null; try { outDate = fmt.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } return outDate; }
public static Date parseUnixTSToDateTime(String dateString) { try { long unixSeconds = Long.valueOf(dateString); Date date = new Date(unixSeconds * 1000L); // *1000 is to convert seconds to milliseconds return DATE_FORMAT.parse(DATE_FORMAT.format(date)); } catch (ParseException e) { throw new ExchangeException("Illegal date/time format", e); } } }
protected CompareTupleFilter buildINCompareFilter(TblColRef dateColumn) throws ParseException { CompareTupleFilter compareFilter = new CompareTupleFilter(FilterOperatorEnum.IN); ColumnTupleFilter columnFilter = new ColumnTupleFilter(dateColumn); compareFilter.addChild(columnFilter); List<String> inValues = Lists.newArrayList(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT); Date startDate = simpleDateFormat.parse("1970-01-01"); Date endDate = simpleDateFormat.parse("2100-01-01"); Calendar start = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ROOT); start.setTime(startDate); Calendar end = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ROOT); end.setTime(endDate); for (Date date = start.getTime(); start.before(end); start.add(Calendar.DATE, 1), date = start.getTime()) { inValues.add(simpleDateFormat.format(date)); } ConstantTupleFilter constantFilter = new ConstantTupleFilter(inValues); compareFilter.addChild(constantFilter); return compareFilter; }
@Test public void calendarConversionsDifferingTimeZones() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss SSS"); Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC+0")); calendar.setTime(sdf.parse("20180115 153256 001")); }
private long parseDateValue(@Nullable String headerValue) { if (headerValue == null) { // No header value sent at all return -1; } if (headerValue.length() >= 3) { // Short "0" or "-1" like values are never valid HTTP date headers... // Let's only bother with SimpleDateFormat parsing for long enough values. for (String dateFormat : DATE_FORMATS) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US); simpleDateFormat.setTimeZone(GMT); try { return simpleDateFormat.parse(headerValue).getTime(); } catch (ParseException ex) { // ignore } } } return -1; }
@Override public Object decode(Object jv) throws IOException { if (jv instanceof String) { try { return new SimpleDateFormat(DATE_FORMAT).parse((String) jv); } catch (ParseException e) { throw new IllegalArgumentException(e.getMessage(), e); } } if (jv instanceof Number) { return new Date(((Number) jv).longValue()); } return (Date) null; } };
private static long parseIso8601(String value) { try { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(value).getTime(); } catch (ParseException ignored) { return 0; } }
public static String toDateTimeFormat(String input, String inputfmt, String outputfmt) { Date date = null; SimpleDateFormat infmt = new SimpleDateFormat(inputfmt); try { date = infmt.parse(input); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat outFormat = new SimpleDateFormat(outputfmt); return outFormat.format(date); }
public static Date castToDate(String text) { if (text == null || text.length() == 0) { return null; } String format; if (text.length() == "yyyy-MM-dd".length()) { format = "yyyy-MM-dd"; } else { format = "yyyy-MM-dd HH:mm:ss"; } try { return new SimpleDateFormat(format).parse(text); } catch (ParseException e) { throw new DruidRuntimeException("format : " + format + ", value : " + text, e); } }
public static long parseGMTToMillis(String gmtTime) throws ParseException { if (TextUtils.isEmpty(gmtTime)) return 0; SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_HTTP_DATA, Locale.US); formatter.setTimeZone(GMT_TIME_ZONE); Date date = formatter.parse(gmtTime); return date.getTime(); }
public Date getTimestamp() { try { // Parse the timestamp into a Date object return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.getDefault()) .parse(timestamp); } catch (IllegalArgumentException | ParseException e) { // Return current Date return new Date(); } }
@Override public Date fromString(String string) { try { return new Timestamp( new SimpleDateFormat( TIMESTAMP_FORMAT ).parse( string ).getTime() ); } catch ( ParseException pe) { throw new HibernateException( "could not parse timestamp string" + string, pe ); } }
/** * 获取上一分钟的字符串 * * @param currentMinuteStamp * @return */ private String getLastMinute(String currentMinuteStamp) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date currentDate = sdf.parse(currentMinuteStamp); Date lastMinute = DateUtils.addMinutes(currentDate, -1); return sdf.format(lastMinute); } catch (ParseException e) { logger.error(e.getMessage(), e); return null; } }