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

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

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

DateTimeFormatter.withChronology介绍

[英]Returns a copy of this formatter with a new override chronology.

This returns a formatter with similar state to this formatter but with the override chronology set. By default, a formatter has no override chronology, returning null.

If an override is added, then any date that is printed or parsed will be affected.

When printing, if the Temporal object contains a date then it will be converted to a date in the override chronology. Any time or zone will be retained unless overridden. The converted result will behave in a manner equivalent to an implementation of ChronoLocalDate, ChronoLocalDateTime or ChronoZonedDateTime.

When parsing, the override chronology will be used to interpret the ChronoField into a date unless the formatter directly parses a valid chronology.

This instance is immutable and unaffected by this method call.
[中]返回此格式化程序的副本,其中包含一个新的覆盖年表。
这将返回一个与此格式化程序状态相似但具有覆盖时间序列集的格式化程序。默认情况下,格式化程序没有覆盖时间顺序,返回null。
如果添加了覆盖,则打印或解析的任何日期都将受到影响。
打印时,如果时间对象包含日期,则它将转换为覆盖年表中的日期。任何时间或区域将被保留,除非被覆盖。转换后的结果将以相当于ChronoLocalDate、ChronoLocalDateTime或ChronoZonedDateTime的实现的方式运行。
解析时,除非格式化程序直接解析有效的年表,否则重写年表将用于将ChronoField解释为日期。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: jfoenixadmin/JFoenix

private String formatMonth(YearMonth yearMonth) {
  try {
    Chronology chrono = getPrimaryChronology();
    ChronoLocalDate cDate = chrono.date(yearMonth.atDay(1));
    return monthFormatter.withLocale(getLocale()).withChronology(chrono).format(cDate);
  } catch (DateTimeException ex) {
    // Date is out of range.
    return "";
  }
}

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

/**
 * Get the DateTimeFormatter with the this context's settings
 * applied to the base {@code formatter}.
 * @param formatter the base formatter that establishes default
 * formatting rules, generally context-independent
 * @return the contextual DateTimeFormatter
 */
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
  if (this.chronology != null) {
    formatter = formatter.withChronology(this.chronology);
  }
  if (this.timeZone != null) {
    formatter = formatter.withZone(this.timeZone);
  }
  else {
    LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
    if (localeContext instanceof TimeZoneAwareLocaleContext) {
      TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
      if (timeZone != null) {
        formatter = formatter.withZone(timeZone.toZoneId());
      }
    }
  }
  return formatter;
}

代码示例来源:origin: jfoenixadmin/JFoenix

private String formatYear(YearMonth yearMonth) {
  try {
    Chronology chrono = getPrimaryChronology();
    ChronoLocalDate cDate = chrono.date(yearMonth.atDay(1));
    return yearFormatter.withLocale(getLocale())
      .withChronology(chrono)
      .withDecimalStyle(DecimalStyle.of(getLocale()))
      .format(cDate);
  } catch (DateTimeException ex) {
    // Date is out of range.
    return "";
  }
}

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

/**
 * Get the DateTimeFormatter with the this context's settings
 * applied to the base {@code formatter}.
 * @param formatter the base formatter that establishes default
 * formatting rules, generally context-independent
 * @return the contextual DateTimeFormatter
 */
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
  if (this.chronology != null) {
    formatter = formatter.withChronology(this.chronology);
  }
  if (this.timeZone != null) {
    formatter = formatter.withZone(this.timeZone);
  }
  else {
    LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
    if (localeContext instanceof TimeZoneAwareLocaleContext) {
      TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
      if (timeZone != null) {
        formatter = formatter.withZone(timeZone.toZoneId());
      }
    }
  }
  return formatter;
}

代码示例来源:origin: jfoenixadmin/JFoenix

.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(locale))
.format(cDate);

代码示例来源:origin: com.jfoenix/jfoenix

private String formatMonth(YearMonth yearMonth) {
  try {
    Chronology chrono = getPrimaryChronology();
    ChronoLocalDate cDate = chrono.date(yearMonth.atDay(1));
    return monthFormatter.withLocale(getLocale()).withChronology(chrono).format(cDate);
  } catch (DateTimeException ex) {
    // Date is out of range.
    return "";
  }
}

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

/**
 * Returns a locale specific date format.
 * <p>
 * This returns a formatter that will print/parse a date.
 * The exact format pattern used varies by locale.
 * <p>
 * The locale is determined from the formatter. The formatter returned directly by
 * this method will use the {@link Locale#getDefault() default locale}.
 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
 * on the result of this method.
 * <p>
 * Note that the localized pattern is looked up lazily.
 * This {@code DateTimeFormatter} holds the style required and the locale,
 * looking up the pattern required on demand.
 *
 * @param dateStyle  the formatter style to obtain, not null
 * @return the date formatter, not null
 */
public static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) {
  Jdk8Methods.requireNonNull(dateStyle, "dateStyle");
  return new DateTimeFormatterBuilder().appendLocalized(dateStyle, null)
          .toFormatter().withChronology(IsoChronology.INSTANCE);
}

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

/**
 * Returns a locale specific time format.
 * <p>
 * This returns a formatter that will print/parse a time.
 * The exact format pattern used varies by locale.
 * <p>
 * The locale is determined from the formatter. The formatter returned directly by
 * this method will use the {@link Locale#getDefault() default locale}.
 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
 * on the result of this method.
 * <p>
 * Note that the localized pattern is looked up lazily.
 * This {@code DateTimeFormatter} holds the style required and the locale,
 * looking up the pattern required on demand.
 *
 * @param timeStyle  the formatter style to obtain, not null
 * @return the time formatter, not null
 */
public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) {
  Jdk8Methods.requireNonNull(timeStyle, "timeStyle");
  return new DateTimeFormatterBuilder().appendLocalized(null, timeStyle)
          .toFormatter().withChronology(IsoChronology.INSTANCE);
}

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

/**
 * Returns a locale specific date-time format, which is typically of short length.
 * <p>
 * This returns a formatter that will print/parse a date-time.
 * The exact format pattern used varies by locale.
 * <p>
 * The locale is determined from the formatter. The formatter returned directly by
 * this method will use the {@link Locale#getDefault() default locale}.
 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
 * on the result of this method.
 * <p>
 * Note that the localized pattern is looked up lazily.
 * This {@code DateTimeFormatter} holds the style required and the locale,
 * looking up the pattern required on demand.
 *
 * @param dateTimeStyle  the formatter style to obtain, not null
 * @return the date-time formatter, not null
 */
public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle) {
  Jdk8Methods.requireNonNull(dateTimeStyle, "dateTimeStyle");
  return new DateTimeFormatterBuilder().appendLocalized(dateTimeStyle, dateTimeStyle)
          .toFormatter().withChronology(IsoChronology.INSTANCE);
}

代码示例来源:origin: com.jfoenix/jfoenix

private String formatYear(YearMonth yearMonth) {
  try {
    Chronology chrono = getPrimaryChronology();
    ChronoLocalDate cDate = chrono.date(yearMonth.atDay(1));
    return yearFormatter.withLocale(getLocale())
      .withChronology(chrono)
      .withDecimalStyle(DecimalStyle.of(getLocale()))
      .format(cDate);
  } catch (DateTimeException ex) {
    // Date is out of range.
    return "";
  }
}

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

/**
 * Get the DateTimeFormatter with the this context's settings
 * applied to the base {@code formatter}.
 * @param formatter the base formatter that establishes default
 * formatting rules, generally context-independent
 * @return the contextual DateTimeFormatter
 */
public DateTimeFormatter getFormatter(DateTimeFormatter formatter) {
  if (this.chronology != null) {
    formatter = formatter.withChronology(this.chronology);
  }
  if (this.timeZone != null) {
    formatter = formatter.withZone(this.timeZone);
  }
  else {
    LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
    if (localeContext instanceof TimeZoneAwareLocaleContext) {
      TimeZone timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
      if (timeZone != null) {
        formatter = formatter.withZone(timeZone.toZoneId());
      }
    }
  }
  return formatter;
}

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

/**
 * Returns a locale specific date and time format.
 * <p>
 * This returns a formatter that will print/parse a date-time.
 * The exact format pattern used varies by locale.
 * <p>
 * The locale is determined from the formatter. The formatter returned directly by
 * this method will use the {@link Locale#getDefault() default locale}.
 * The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
 * on the result of this method.
 * <p>
 * Note that the localized pattern is looked up lazily.
 * This {@code DateTimeFormatter} holds the style required and the locale,
 * looking up the pattern required on demand.
 *
 * @param dateStyle  the date formatter style to obtain, not null
 * @param timeStyle  the time formatter style to obtain, not null
 * @return the date, time or date-time formatter, not null
 */
public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) {
  Jdk8Methods.requireNonNull(dateStyle, "dateStyle");
  Jdk8Methods.requireNonNull(timeStyle, "timeStyle");
  return new DateTimeFormatterBuilder().appendLocalized(dateStyle, timeStyle)
          .toFormatter().withChronology(IsoChronology.INSTANCE);
}

代码示例来源:origin: com.jfoenix/jfoenix

.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(locale))
.format(cDate);

代码示例来源: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);
}

相关文章