private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) { return dateTimeFormatter.withLocale(Locale.US); }
/** * 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); }
/** * 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); }
@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; } }
@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; } }
@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; } }
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); } }
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 ""; } }
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); } } }
@Test public void testDateFormaterWithLocaleEN() { String anotherDate = "12-May-2015"; LocalDate result = LocalDate.parse(anotherDate, DateParser.DEFAULT_FORMATTER.withLocale(Locale.ENGLISH)); assertThat(result, notNullValue()); }
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))); } }
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 ""; } }
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);
@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); }
String cellText = dayCellFormatter.withLocale(locale) .withChronology(chrono) .withDecimalStyle(DecimalStyle.of(locale))
private static DateTimeFormatter getSafeDateTimeFormatter(final String pattern) { DateTimeFormatter safeFormatter = getDateTimeFormatter(pattern); if (Locale.UK.equals(safeFormatter.getLocale())) { return safeFormatter.withLocale(Locale.ENGLISH); } return safeFormatter; }
@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); }
@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); }
@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); }
@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); }