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

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

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

DateTimeFormatter.withLocale介绍

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

This is used to lookup any part of the formatter needing specific localization, such as the text or localized pattern.

This instance is immutable and unaffected by this method call.
[中]返回具有新区域设置的此格式化程序的副本。
这用于查找需要特定本地化的格式化程序的任何部分,例如文本或本地化模式。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) {
  return dateTimeFormatter.withLocale(Locale.US);
}

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

/**
 * Obtain a DateTimeFormatter with user-specific settings applied to the given base Formatter.
 * @param formatter the base formatter that establishes default formatting rules
 * (generally user independent)
 * @param locale the current user locale (may be {@code null} if not known)
 * @return the user-specific DateTimeFormatter
 */
public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, @Nullable Locale locale) {
  DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter);
  DateTimeContext context = getDateTimeContext();
  return (context != null ? context.getFormatter(formatterToUse) : formatterToUse);
}

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

/**
 * Obtain a DateTimeFormatter with user-specific settings applied to the given base Formatter.
 * @param formatter the base formatter that establishes default formatting rules
 * (generally user independent)
 * @param locale the current user locale (may be {@code null} if not known)
 * @return the user-specific DateTimeFormatter
 */
public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, @Nullable Locale locale) {
  DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter);
  DateTimeContext context = getDateTimeContext();
  return (context != null ? context.getFormatter(formatterToUse) : formatterToUse);
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public boolean canParse(String s) {
  if (isMissing(s)) {
    return true;
  }
  try {
    LocalDateTime.parse(s, formatter.withLocale(locale));
    return true;
  } catch (DateTimeParseException e) {
    // it's all part of the plan
    return false;
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public boolean canParse(String s) {
  if (isMissing(s)) {
    return true;
  }
  try {
    LocalDate.parse(s, formatter.withLocale(locale));
    return true;
  } catch (DateTimeParseException e) {
    // it's all part of the plan
    return false;
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public boolean canParse(String s) {
  if (isMissing(s)) {
    return true;
  }
  try {
    LocalTime.parse(s, formatter.withLocale(locale));
    return true;
  } catch (DateTimeParseException e) {
    // it's all part of the plan
    return false;
  }
}

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

private void updateDayNameCells() {
  int weekFirstDay = WeekFields.of(getLocale()).getFirstDayOfWeek().getValue();
  LocalDate date = LocalDate.of(2009, 7, 12 + weekFirstDay);
  for (int i = 0; i < daysPerWeek; i++) {
    String name = weekDayNameFormatter.withLocale(getLocale()).format(date.plus(i, DAYS));
    // Fix Chinese environment week display incorrectly
    // Take the last character of the Chinese weekday names
    if (weekDayNameFormatter.getLocale() == java.util.Locale.CHINA) {
      name = name.substring(name.length() - 1).toUpperCase();
    } else {
      name = name.substring(0, 1).toUpperCase();
    }
    weekDaysCells.get(i).setText(name);
  }
}

代码示例来源: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: jfoenixadmin/JFoenix

void updateWeekNumberDateCells() {
  if (datePicker.isShowWeekNumbers()) {
    final Locale locale = getLocale();
    LocalDate firstDayOfMonth = selectedYearMonth.get().atDay(1);
    for (int i = 0; i < 6; i++) {
      LocalDate date = firstDayOfMonth.plus(i, WEEKS);
      String weekNumber = weekNumberFormatter.withLocale(locale)
        .withDecimalStyle(DecimalStyle.of(locale))
        .format(date);
      weekNumberCells.get(i).setText(weekNumber);
    }
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Test
public void testDateFormaterWithLocaleEN() {
  String anotherDate = "12-May-2015";
  LocalDate result = LocalDate.parse(anotherDate, DateParser.DEFAULT_FORMATTER.withLocale(Locale.ENGLISH));
  assertThat(result, notNullValue());
}

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

void updateValue() {
  if (is24HourView) {
    LocalTimeStringConverter localTimeStringConverter =
      new LocalTimeStringConverter(FormatStyle.SHORT, Locale.GERMAN);
    timePicker.setValue(localTimeStringConverter.fromString(selectedHourLabel.getText()
                                + ":" + selectedMinLabel.getText()));
  } else {
    timePicker.setValue(LocalTime.parse(selectedHourLabel.getText() + ":" + selectedMinLabel.getText() + " " + period.get(), DateTimeFormatter.ofPattern("h:mm a").withLocale(Locale.ENGLISH)));
  }
}

代码示例来源: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: com.thoughtworks.xstream/xstream

final TemporalAccessor ta = ISO_WEEK_DATE_TIME.withLocale(Locale.getDefault()).parse(str);
  final Year y = Year.from(ta);
  final MonthDay md = MonthDay.from(ta);
  final TemporalAccessor ta = BASIC_WEEK_DATE_TIME.withLocale(Locale.getDefault()).parse(str);
  final Year y = Year.from(ta);
  final MonthDay md = MonthDay.from(ta);
  final LocalDate ld = LocalDate.parse(str, DateTimeFormatter.ISO_WEEK_DATE.withLocale(Locale.getDefault()));
  return GregorianCalendar.from(ld.atStartOfDay(ZoneId.systemDefault()));
} catch (final DateTimeParseException e) {
  final TemporalAccessor ta = BASIC_WEEK_DATE.withLocale(Locale.getDefault()).parse(str);
  final Year y = Year.from(ta);
  final MonthDay md = MonthDay.from(ta);
  final TemporalAccessor ta = STD_YEAR_WEEK.withLocale(Locale.getDefault()).parse(str);
  final int y = ta.get(WeekFields.ISO.weekBasedYear());
  final int w = ta.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public DateFormatter withLocale(Locale locale) {
  // shortcurt to not create new objects unnecessarily
  if (locale.equals(parsers[0].getLocale())) {
    return this;
  }
  final DateTimeFormatter[] parsersWithZone = new DateTimeFormatter[parsers.length];
  for (int i = 0; i < parsers.length; i++) {
    parsersWithZone[i] = parsers[i].withLocale(locale);
  }
  return new JavaDateFormatter(format, printer.withLocale(locale), parsersWithZone);
}

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

String cellText = dayCellFormatter.withLocale(locale)
  .withChronology(chrono)
  .withDecimalStyle(DecimalStyle.of(locale))

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

private static DateTimeFormatter getSafeDateTimeFormatter(final String pattern) {
  DateTimeFormatter safeFormatter = getDateTimeFormatter(pattern);
  if (Locale.UK.equals(safeFormatter.getLocale())) {
    return safeFormatter.withLocale(Locale.ENGLISH);
  }
  return safeFormatter;
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
protected String formatDate(LocalDateTime value) {
  if (value == null) {
    return "";
  }
  DateTimeFormatter dateTimeFormatter = DateTimeFormatter
      .ofLocalizedDateTime(FormatStyle.SHORT);
  Locale locale = getLocale();
  if (locale != null) {
    dateTimeFormatter = dateTimeFormatter.withLocale(locale);
  }
  return value.format(dateTimeFormatter);
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
protected String formatDate(LocalDate value) {
  if (value == null) {
    return "";
  }
  DateTimeFormatter dateTimeFormatter = DateTimeFormatter
      .ofLocalizedDate(FormatStyle.SHORT);
  Locale locale = getLocale();
  if (locale != null) {
    dateTimeFormatter = dateTimeFormatter.withLocale(locale);
  }
  return value.format(dateTimeFormatter);
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
public JsonValue encode(LocalDateTime value) {
  String dateString;
  if (value == null) {
    dateString = getNullRepresentation();
  } else if (getLocaleFromGrid) {
    if (getParentGrid() == null) {
      throw new IllegalStateException(
          "Could not find a locale to format with: "
              + "this renderer should either be attached to a grid "
              + "or constructed with locale information");
    }
    dateString = value.format(formatterSupplier.get()
        .withLocale(getParentGrid().getLocale()));
  } else {
    dateString = value.format(formatterSupplier.get());
  }
  return encode(dateString, String.class);
}

代码示例来源:origin: com.vaadin/vaadin-server

@Override
public JsonValue encode(LocalDate value) {
  String dateString;
  if (value == null) {
    dateString = getNullRepresentation();
  } else if (getLocaleFromGrid) {
    if (getParentGrid() == null) {
      throw new IllegalStateException(
          "Could not find a locale to format with: "
              + "this renderer should either be attached to a grid "
              + "or constructed with locale information");
    }
    dateString = value.format(formatterSupplier.get()
        .withLocale(getParentGrid().getLocale()));
  } else {
    dateString = value.format(formatterSupplier.get());
  }
  return encode(dateString, String.class);
}

相关文章