java.time.format.DateTimeFormatter.withResolverStyle()方法的使用及代码示例

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

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

DateTimeFormatter.withResolverStyle介绍

[英]Returns a copy of this formatter with a new resolver style.

This returns a formatter with similar state to this formatter but with the resolver style set. By default, a formatter has the ResolverStyle#SMART resolver style.

Changing the resolver style only has an effect during parsing. Parsing a text string occurs in two phases. Phase 1 is a basic text parse according to the fields added to the builder. Phase 2 resolves the parsed field-value pairs into date and/or time objects. The resolver style is used to control how phase 2, resolving, happens. See ResolverStyle for more information on the options available.

This instance is immutable and unaffected by this method call.
[中]返回具有新解析程序样式的此格式化程序的副本。
这将返回一个与此格式化程序状态相似但具有冲突解决程序样式集的格式化程序。默认情况下,格式化程序具有ResolderStyle#SMART resolver样式。
更改解析器样式仅在解析期间有效。分析文本字符串分两个阶段进行。阶段1是根据添加到生成器中的字段进行基本文本解析。阶段2将解析的字段值对解析为日期和/或时间对象。分解器样式用于控制第2阶段(分解)的发生方式。有关可用选项的更多信息,请参见ResolverStyle。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);

代码示例来源:origin: org.springframework/spring-context

dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);

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

static TimestampParserJava of(final String pattern, final ZoneOffset defaultZoneOffset) {
  // Default parsers from patterns accept strings case-insensitively.
  return new TimestampParserJava(new DateTimeFormatterBuilder()
                      .parseCaseInsensitive()
                      .appendPattern(pattern)
                      .toFormatter(Locale.ENGLISH)
                      .withResolverStyle(ResolverStyle.STRICT),
                  defaultZoneOffset,
                  pattern);
}

代码示例来源:origin: stackoverflow.com

import java.time.*;
import java.time.format.*;
import java.util.*;

public class Test {

  public static void main(String[] args) {
    String dateFormat = "HH:mm:ss MM/dd/uuuu";
    String dateString = "11:30:59 02/31/2015";
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter
      .ofPattern(dateFormat, Locale.US)
      .withResolverStyle(ResolverStyle.STRICT);
    try {
      LocalDateTime date = LocalDateTime.parse(dateString, dateTimeFormatter);
      System.out.println(date);
    } catch (DateTimeParseException e) {
      // Throw invalid date message
      System.out.println("Exception was thrown");
    }
  }
}

代码示例来源:origin: stackoverflow.com

DateTimeFormatter f = ...  // obtain a formatter somehow
DateTimeFormatter smartMode = f.withResolverStyle(ResolverStyle.SMART);

// for example
f = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withResolverStyle(ResolverStyle.SMART);

代码示例来源:origin: stackoverflow.com

DateTimeFormatter tf = DateTimeFormatter.ofPattern("H:m a", Locale.US);
tf = tf.withResolverStyle(ResolverStyle.STRICT);
LocalTime.parse("1:4 PM", tf);
// DateTimeException: Cross check failed: AmPmOfDay 0 vs AmPmOfDay 1

代码示例来源:origin: com.github.seratch/java-time-backport

DateTimeFormatter toFormatter(ResolverStyle style) {
  return toFormatter().withResolverStyle(style);
}

代码示例来源:origin: yu-iskw/bigquery-to-datastore

/**
 * Parse string on date format
 *
 * Note: SimpledateFormat("yyyy-MM-dd") can't help parseing "yyyy-MM-dd HH:mm:ss" as well.
 */
public static Instant parseDate(String value) {
 Instant instant = null;
 try {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d")
    .withResolverStyle(ResolverStyle.SMART);
  java.time.LocalDate localDate = java.time.LocalDate.parse(value, formatter);
  instant = localDate.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant();
 } catch (DateTimeParseException e) {
  // Do nothing.
  ;
 }
 return instant;
}

代码示例来源:origin: ccavanaugh/jgnash

public static void setDateFormatPattern(@NotNull final String pattern) throws IllegalArgumentException {
  Objects.requireNonNull(pattern);
  final Preferences preferences = Preferences.userNodeForPackage(DateUtils.class);
  preferences.put(DATE_FORMAT, pattern);
  shortDateFormatter = DateTimeFormatter.ofPattern(pattern).withResolverStyle(ResolverStyle.SMART);
  shortDateManualEntryFormatter = DateTimeFormatter.ofPattern(getShortDateManualEntryPattern())
      .withResolverStyle(ResolverStyle.SMART);
}

代码示例来源:origin: yu-iskw/bigquery-to-datastore

try {
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern)
   .withResolverStyle(ResolverStyle.SMART);
 java.time.LocalDateTime localDateTime = java.time.LocalDateTime.parse(value, formatter);
 instant = localDateTime.atZone(ZoneId.of("UTC")).toInstant();

代码示例来源:origin: apache/servicemix-bundles

dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);

代码示例来源:origin: com.google.enterprise.cloudsearch/google-cloudsearch-indexing-connector-sdk

private static DateTimeFormatter getIsoDateTime(char delimiter) {
 return new DateTimeFormatterBuilder()
   .parseCaseInsensitive()
   .append(DateTimeFormatter.ISO_LOCAL_DATE)
   .optionalStart()
   .appendLiteral(delimiter)
   .append(DateTimeFormatter.ISO_LOCAL_TIME)
   .optionalEnd()
   .optionalStart()
   .parseLenient()
   .appendOffsetId()
   .parseStrict()
   .optionalStart()
   .appendLiteral('[')
   .parseCaseSensitive()
   .appendZoneRegionId()
   .appendLiteral(']')
   .parseStrict()
   .toFormatter()
   .withResolverStyle(ResolverStyle.STRICT)
   .withChronology(IsoChronology.INSTANCE);
}

代码示例来源:origin: apache/servicemix-bundles

DateTimeFormatter.ISO_LOCAL_DATE.withResolverStyle(ResolverStyle.LENIENT));

相关文章