java.text.SimpleDateFormat.parse()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.7k)|赞(0)|评价(0)|浏览(190)

本文整理了Java中java.text.SimpleDateFormat.parse()方法的一些代码示例,展示了SimpleDateFormat.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SimpleDateFormat.parse()方法的具体详情如下:
包路径:java.text.SimpleDateFormat
类名称:SimpleDateFormat
方法名:parse

SimpleDateFormat.parse介绍

[英]Parses a date from the specified string starting at the index specified by position. If the string is successfully parsed then the index of the ParsePosition is updated to the index following the parsed text. On error, the index is unchanged and the error index of ParsePosition is set to the index where the error occurred.
[中]从位置指定的索引处开始分析指定字符串中的日期。如果成功解析字符串,则ParsePosition的索引将更新为解析文本后面的索引。出错时,索引保持不变,ParsePosition的错误索引设置为发生错误的索引。

代码示例

代码示例来源:origin: netty/netty

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;
  }
}

代码示例来源:origin: spring-projects/spring-framework

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");
}

代码示例来源:origin: apache/rocketmq

public static Date parseDate(String date, String pattern) {
  SimpleDateFormat df = new SimpleDateFormat(pattern);
  try {
    return df.parse(date);
  } catch (ParseException e) {
    return null;
  }
}

代码示例来源:origin: apache/incubator-dubbo

@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;
  }
};

代码示例来源:origin: apache/incubator-gobblin

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;
}

代码示例来源:origin: knowm/XChange

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);
  }
 }
}

代码示例来源:origin: redisson/redisson

/**
 * Parses the HTTP date/time format. Returns <code>-1</code> if given string
 * is invalid.
 */
public static long parseHttpTime(String time) {
  if (time == null) {
    return -1;
  }
  try {
    return TimeUtil.HTTP_DATE_FORMAT.parse(time).getTime();
  }
  catch (ParseException e) {
    return -1;
  }
}

代码示例来源:origin: apache/kylin

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;
}

代码示例来源:origin: knowm/XChange

public static Date toDate(String dateString) {
 try {
  return dateParserNoMillis().parse(dateString);
 } catch (ParseException e) {
  OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateString);
  return new Date(offsetDateTime.toInstant().toEpochMilli());
 }
}

代码示例来源:origin: apache/groovy

@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"));
}

代码示例来源:origin: spring-projects/spring-framework

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;
}

代码示例来源:origin: apache/incubator-dubbo

@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;
  }
};

代码示例来源:origin: redisson/redisson

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;
  }
}

代码示例来源:origin: apache/incubator-gobblin

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);
}

代码示例来源:origin: log4j/log4j

private long parseDate(String record) {
 try {
  String s = parseAttribute(DATE_DELIMITER, record);
  if (s == null) {
   return 0;
  }
  Date d = _sdf.parse(s);
  return d.getTime();
 } catch (ParseException e) {
  return 0;
 }
}

代码示例来源:origin: alibaba/druid

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);
  }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

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();
}

代码示例来源:origin: knowm/XChange

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();
 }
}

代码示例来源:origin: hibernate/hibernate-orm

@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 );
  }
}

代码示例来源:origin: sohutv/cachecloud

/**
 * 获取上一分钟的字符串
 * 
 * @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;
  }
}

相关文章

SimpleDateFormat类方法