本文整理了Java中java.time.format.DateTimeFormatter.parseUnresolved()
方法的一些代码示例,展示了DateTimeFormatter.parseUnresolved()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter.parseUnresolved()
方法的具体详情如下:
包路径:java.time.format.DateTimeFormatter
类名称:DateTimeFormatter
方法名:parseUnresolved
[英]Parses the text using this formatter, without resolving the result, intended for advanced use cases.
Parsing is implemented as a two-phase operation. First, the text is parsed using the layout defined by the formatter, producing a Map of field to value, a ZoneId and a Chronology. Second, the parsed data is resolved, by validating, combining and simplifying the various fields into more useful ones. This method performs the parsing stage but not the resolving stage.
The result of this method is TemporalAccessor which represents the data as seen in the input. Values are not validated, thus parsing a date string of '2012-00-65' would result in a temporal with three fields - year of '2012', month of '0' and day-of-month of '65'.
The text will be parsed from the specified start ParsePosition. The entire length of the text does not have to be parsed, the ParsePositionwill be updated with the index at the end of parsing.
Errors are returned using the error index field of the ParsePositioninstead of DateTimeParseException. The returned error index will be set to an index indicative of the error. Callers must check for errors before using the context.
If the formatter parses the same field more than once with different values, the result will be an error.
This method is intended for advanced use cases that need access to the internal state during parsing. Typical application code should use #parse(CharSequence,TemporalQuery) or the parse method on the target type.
[中]使用此格式化程序解析文本,而不解析结果,用于高级用例。
解析是作为一个两阶段操作实现的。首先,使用格式化程序定义的布局解析文本,生成字段到值的映射、ZoneId和年表。其次,通过验证、组合和简化各个字段,将解析的数据解析。此方法执行解析阶段,但不执行解析阶段。
此方法的结果是TemporalAccessor,它表示在输入中看到的数据。值未经验证,因此解析日期字符串“2012-00-65”将导致一个带有三个字段的临时字段-年份“2012”、月份“0”和月份“65”。
将从指定的开始解析位置解析文本。不必解析整个文本长度,ParsePosition将在解析结束时使用索引进行更新。
使用ParsePosition的错误索引字段而不是DateTimeParseException返回错误。返回的错误索引将设置为指示错误的索引。调用方必须在使用上下文之前检查错误。
如果格式化程序使用不同的值多次解析同一字段,则结果将是错误的。
此方法适用于需要在解析期间访问内部状态的高级用例。典型的应用程序代码应该对目标类型使用#parse(CharSequence,TemporalQuery)或parse方法。
代码示例来源:origin: oracle/helidon
private static DateTimeFormatter buildDateTimeFormatter(String stringValue) {
/*
A Java 8 bug causes DateTimeFormatter.withZone to override an explicit
time zone in the parsed string, contrary to the documented behavior. So
if the string includes a zone do NOT use withZone in building the formatter.
*/
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
ParsePosition pp = new ParsePosition(0);
TemporalAccessor accessor = formatter.parseUnresolved(stringValue, pp);
if (!accessor.isSupported(ChronoField.OFFSET_SECONDS)) {
formatter = formatter.withZone(ZoneId.of("UTC"));
}
return formatter;
}
代码示例来源:origin: io.helidon.config/helidon-config
private static DateTimeFormatter buildDateTimeFormatter(String stringValue) {
/*
A Java 8 bug causes DateTimeFormatter.withZone to override an explicit
time zone in the parsed string, contrary to the documented behavior. So
if the string includes a zone do NOT use withZone in building the formatter.
*/
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
ParsePosition pp = new ParsePosition(0);
TemporalAccessor accessor = formatter.parseUnresolved(stringValue, pp);
if (!accessor.isSupported(ChronoField.OFFSET_SECONDS)) {
formatter = formatter.withZone(ZoneId.of("UTC"));
}
return formatter;
}
代码示例来源:origin: org.threeten/threeten-extra
try {
ParsePosition pp = new ParsePosition(0);
formatter.parseUnresolved(text, pp);
int len = text.length();
if (pp.getErrorIndex() == -1 && pp.getIndex() == len) {
代码示例来源:origin: com.github.seratch/java-time-backport
private int parseSecs(String str) {
if (str.equals("-")) {
return 0;
}
int pos = 0;
if (str.startsWith("-")) {
pos = 1;
}
ParsePosition pp = new ParsePosition(pos);
TemporalAccessor parsed = TIME_PARSER.parseUnresolved(str, pp);
if (parsed == null || pp.getErrorIndex() >= 0) {
throw new IllegalArgumentException(str);
}
long hour = parsed.getLong(HOUR_OF_DAY);
Long min = (parsed.isSupported(MINUTE_OF_HOUR) ? parsed.getLong(MINUTE_OF_HOUR) : null);
Long sec = (parsed.isSupported(SECOND_OF_MINUTE) ? parsed.getLong(SECOND_OF_MINUTE) : null);
int secs = (int) (hour * 60 * 60 + (min != null ? min : 0) * 60 + (sec != null ? sec : 0));
if (pos == 1) {
secs = -secs;
}
return secs;
}
代码示例来源:origin: com.sqlapp/sqlapp-core
protected Temporal parseTemporal(String value, DateTimeFormatter dateTimeFormatter) {
ParsePosition position = new ParsePosition(0);
TemporalAccessor temporalAccessor = dateTimeFormatter.parseUnresolved(value, position);
if (position.getErrorIndex()<0){
int year=getYear(temporalAccessor);
内容来源于网络,如有侵权,请联系作者删除!