@Test public void test_standaloneNormal() { assertEquals(TextStyle.FULL, TextStyle.FULL_STANDALONE.asNormal()); assertEquals(TextStyle.SHORT, TextStyle.SHORT.asNormal()); assertEquals(TextStyle.NARROW, TextStyle.NARROW.asNormal()); assertEquals(TextStyle.FULL_STANDALONE, TextStyle.FULL_STANDALONE.asStandalone()); assertEquals(TextStyle.SHORT_STANDALONE, TextStyle.SHORT.asStandalone()); assertEquals(TextStyle.NARROW_STANDALONE, TextStyle.NARROW.asStandalone()); assertTrue(TextStyle.FULL_STANDALONE.isStandalone()); assertTrue(TextStyle.SHORT_STANDALONE.isStandalone()); assertTrue(TextStyle.NARROW_STANDALONE.isStandalone()); assertTrue(!TextStyle.FULL.isStandalone()); assertTrue(!TextStyle.SHORT.isStandalone()); assertTrue(!TextStyle.NARROW.isStandalone()); }
@Test(dataProvider="preferredZones") public void test_ParseText(String expected, String text, Set<ZoneId> preferred, Locale locale, TextStyle style) { DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneText(style, preferred) .toFormatter(locale) .withDecimalStyle(DecimalStyle.of(locale)); String ret = fmt.parse(text, TemporalQueries.zone()).getId(); System.out.printf("[%-5s %s] %24s -> %s(%s)%n", locale.toString(), style == TextStyle.FULL ? " full" :"short", text, ret, expected); assertEquals(ret, expected); }
@DataProvider(name="preferredZones") Object[][] data_preferredZones() { return new Object[][] { {"America/New_York", "Eastern Standard Time", none, Locale.ENGLISH, TextStyle.FULL}, // {"EST", "Eastern Standard Time", preferred, Locale.ENGLISH, TextStyle.FULL}, {"Europe/Paris", "Central European Time", none, Locale.ENGLISH, TextStyle.FULL}, {"CET", "Central European Time", preferred, Locale.ENGLISH, TextStyle.FULL}, {"Asia/Shanghai", "China Standard Time", none, Locale.ENGLISH, TextStyle.FULL}, {"Asia/Taipei", "China Standard Time", preferred, Locale.ENGLISH, TextStyle.FULL}, {"America/Chicago", "CST", none, Locale.ENGLISH, TextStyle.SHORT}, {"Asia/Taipei", "CST", preferred, Locale.ENGLISH, TextStyle.SHORT}, {"Australia/South", "ACST", preferred_s, Locale.ENGLISH, TextStyle.SHORT}, {"America/Chicago", "CDT", none, Locale.ENGLISH, TextStyle.SHORT}, {"Asia/Shanghai", "CDT", preferred_s, Locale.ENGLISH, TextStyle.SHORT}, }; }
private void parseText(Set<String> zids, Locale locale, TextStyle style, boolean ci) { System.out.println("---------------------------------------"); DateTimeFormatter fmt = getFormatter(locale, style, ci); for (String[] names : new DateFormatSymbols(locale).getZoneStrings()) { if (!zids.contains(names[0])) { continue; } String zid = names[0]; String expected = ZoneName.toZid(zid, locale); parse(fmt, zid, expected, zid, locale, style, ci); int i = style == TextStyle.FULL ? 1 : 2; for (; i < names.length; i += 2) { parse(fmt, zid, expected, names[i], locale, style, ci); } } }
void test3() { final String[] TZNAMES = { LATIME, PST, PST8PDT, US_PACIFIC, TOKYOTIME, JST, JAPAN, }; for (String tzname : TZNAMES) { TimeZone tz = TimeZone.getTimeZone(tzname); for (int style : new int[] { TimeZone.LONG, TimeZone.SHORT }) { String osakaStd = tz.getDisplayName(false, style, OSAKA); if (osakaStd != null) { String generic = tz.toZoneId().getDisplayName( style == TimeZone.LONG ? TextStyle.FULL : TextStyle.SHORT, GENERIC); String expected = "Generic " + osakaStd; if (!expected.equals(generic)) { throw new RuntimeException("Wrong generic name: got=\"" + generic + "\", expected=\"" + expected + "\""); } } } } }
@Test(dataProvider="parseStandaloneText") public void test_parseStandaloneText(Locale locale, TemporalField field, TextStyle style, int expectedValue, String input) { DateTimeFormatter formatter = getFormatter(field, style).withLocale(locale); ParsePosition pos = new ParsePosition(0); assertEquals(formatter.parseUnresolved(input, pos).getLong(field), (long) expectedValue); assertEquals(pos.getIndex(), input.length()); }
@Test public void test_pattern_String() { DateTimeFormatter test = DateTimeFormatter.ofPattern("d MMM yyyy"); Locale fmtLocale = Locale.getDefault(Locale.Category.FORMAT); assertEquals(test.format(LocalDate.of(2012, 6, 30)), "30 " + Month.JUNE.getDisplayName(TextStyle.SHORT, fmtLocale) + " 2012"); assertEquals(test.getLocale(), fmtLocale, "Locale.Category.FORMAT"); }
@Test(dataProvider="offsetBasedValidPrefix") public void factory_of_String_offsetBasedValid_prefixGMT(String input, String id, String offsetId) { ZoneId test = ZoneId.of("GMT" + input); assertEquals(test.getId(), "GMT" + id); assertEquals(test.getRules(), ZoneOffset.of(offsetId).getRules()); assertEquals(test.normalized(), ZoneOffset.of(offsetId)); assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), displayName("GMT" + id)); assertEquals(test.getRules().isFixedOffset(), true); assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(offsetId)); }
@Override public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeStartObject(); jsonGenerator.writeStringField("text", localDate.toString()); // "2017-06-25" jsonGenerator.writeNumberField("year", localDate.getYear()); jsonGenerator.writeNumberField("month", localDate.getMonthValue()); jsonGenerator.writeNumberField("day", localDate.getDayOfMonth()); jsonGenerator.writeStringField("dayOfWeek", localDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.US)); // "Sunday" jsonGenerator.writeNumberField("dayOfYear", localDate.getDayOfYear()); // 176 jsonGenerator.writeEndObject(); }
public Node createSingleDayHeader(LocalDate date) { final Label lblWeekday = new Label(date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault())); lblWeekday.getStyleClass().add("header-weekday"); final Label lblDate = new Label(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(date)); lblDate.getStyleClass().add("header-date"); VBox container = new VBox(lblDate, lblWeekday); container.getStyleClass().add("header-container"); if(date.equals(LocalDate.now())) { container.getStyleClass().add("header-container-today"); } container.setAlignment(Pos.TOP_CENTER); return container; }
@Test(dataProvider="parseText") public void test_parse_strict_caseInsensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception { setCaseSensitive(false); ParsePosition pos = new ParsePosition(0); assertEquals(getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos).getLong(field), (long) value); assertEquals(pos.getIndex(), input.length()); }
public void test_constant_UTC() { ZoneId test = ZoneOffset.UTC; assertEquals(test.getId(), "Z"); assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), "Z"); assertEquals(test.getRules().isFixedOffset(), true); assertEquals(test.getRules().getOffset(Instant.ofEpochSecond(0L)), ZoneOffset.UTC); checkOffset(test.getRules(), createLDT(2008, 6, 30), ZoneOffset.UTC, 1); }
@Test(dataProvider="offsetBasedValid") public void factory_of_String_offsetBasedValid_noPrefix(String input, String id) { ZoneId test = ZoneId.of(input); assertEquals(test.getId(), id); assertEquals(test, ZoneOffset.of(id)); assertEquals(test.normalized(), ZoneOffset.of(id)); assertEquals(test.getDisplayName(TextStyle.FULL, Locale.UK), id); assertEquals(test.getRules().isFixedOffset(), true); assertEquals(test.getRules().getOffset(Instant.EPOCH), ZoneOffset.of(id)); }
public void test_parse_midStr() throws Exception { ParsePosition pos = new ParsePosition(3); assertEquals(getFormatter(DAY_OF_WEEK, TextStyle.FULL) .parseUnresolved("XxxMondayXxx", pos) .getLong(DAY_OF_WEEK), 1L); assertEquals(pos.getIndex(), 9); }
public LocalTime(OffsetDateTime offsetDateTime, Locale locale) { offset = offsetDateTime.getOffset().getTotalSeconds(); year = offsetDateTime.getYear(); month = offsetDateTime.getMonth().getDisplayName(TextStyle.FULL,locale); dayOfMonth = offsetDateTime.getDayOfMonth(); dayOfWeek = offsetDateTime.getDayOfWeek().getDisplayName(TextStyle.FULL,locale); monthValue = offsetDateTime.getMonthValue(); hour = offsetDateTime.getHour(); minute = offsetDateTime.getMinute(); second = offsetDateTime.getSecond(); nano = offsetDateTime.getNano(); }
@DataProvider(name="print_localized") Object[][] data_print_localized() { return new Object[][] { {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+01:00"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+01:23"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+00:23"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+01:23:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-01:00"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-01:23"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-00:23"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-01:23:45"}, {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+1"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+1:23"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+0:23"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+1:23:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-1"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-1:23"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-0:23"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-1:23:45"}, {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"}, }; }
/** * Gets the textual representation of this chronology. * <p> * This returns the textual name used to identify the chronology. The parameters control the length of the * returned text and the locale. * * @param style the length of the text required, not null * @param locale the locale to use, not null * @return the text value of the chronology, not null */ public String getText(TextStyle style, Locale locale) { // TODO: support at least the style? return toString(); // return new DateTimeFormatterBuilder().appendChronoText(style).toFormatter(locale) // .print(new DefaultInterfaceDateTimeAccessor() { // // @Override // public boolean isSupported(DateTimeField field) { // // return false; // } // // @Override // public long getLong(DateTimeField field) { // // throw new DateTimeException("Unsupported field: " + field); // } // // @SuppressWarnings("unchecked") // @Override // public <R> R query(Query<R> query) { // // if (query == Query.CHRONO) { // return (R) Chrono.this; // } // return super.query(query); // } // }); }
void genericFallbackTest() { String generic = TimeZone.getTimeZone(LATIME) .toZoneId() .getDisplayName(TextStyle.SHORT, OSAKA); if (!PT.equals(generic)) { throw new RuntimeException("Generic name fallback failed. got: "+generic); } }
@Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { long millis = System.currentTimeMillis(); sender.sendMessage(ChatColor.GOLD + "The server time is currently " + ChatColor.YELLOW + DateTimeFormats.DAY_MTH_HR_MIN_AMPM.format(millis) + ChatColor.GOLD + '.'); Map<LocalDateTime, String> scheduleMap = plugin.getEventScheduler().getScheduleMap(); if (scheduleMap.isEmpty()) { sender.sendMessage(ChatColor.RED + "There is not an event schedule for after now."); return true; } LocalDateTime now = LocalDateTime.now(DateTimeFormats.SERVER_ZONE_ID); for (Map.Entry<LocalDateTime, String> entry : scheduleMap.entrySet()) { // Only show the events that haven't been yet. LocalDateTime scheduleDateTime = entry.getKey(); if (now.isAfter(scheduleDateTime)) continue; String monthName = scheduleDateTime.getMonth().getDisplayName(TextStyle.FULL, Locale.ENGLISH); String weekName = scheduleDateTime.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.ENGLISH); sender.sendMessage(ChatColor.DARK_AQUA + WordUtils.capitalizeFully(entry.getValue()) + ChatColor.GRAY + " is the next event: " + ChatColor.AQUA + weekName + ' ' + scheduleDateTime.getDayOfMonth() + ' ' + monthName + ChatColor.DARK_AQUA + " (" + DateTimeFormats.HR_MIN_AMPM.format(TimeUnit.HOURS.toMillis(scheduleDateTime.getHour()) + TimeUnit.MINUTES.toMillis(scheduleDateTime.getMinute())) + ')'); return true; } sender.sendMessage(ChatColor.RED + "There is not an event scheduled after now."); return true; }
@DataProvider(name="print_JapaneseChronology") Object[][] provider_japaneseEra() { return new Object[][] { {ERA, TextStyle.FULL, 2, "Heisei"}, // Note: CLDR doesn't define "wide" Japanese era names. {ERA, TextStyle.SHORT, 2, "Heisei"}, {ERA, TextStyle.NARROW, 2, "H"}, }; }
@Test(dataProvider="print_localized") public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) { OffsetDateTime odt = OffsetDateTime.of(ldt, offset); ZonedDateTime zdt = ldt.atZone(offset); DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style) .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); if (style == TextStyle.FULL) { f = new DateTimeFormatterBuilder().appendPattern("ZZZZ") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); f = new DateTimeFormatterBuilder().appendPattern("OOOO") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } if (style == TextStyle.SHORT) { f = new DateTimeFormatterBuilder().appendPattern("O") .toFormatter(); assertEquals(f.format(odt), expected); assertEquals(f.format(zdt), expected); assertEquals(f.parse(expected, ZoneOffset::from), offset); } }
public void test_parse_noMatch1() throws Exception { ParsePosition pos = new ParsePosition(0); TemporalAccessor parsed = getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Munday", pos); assertEquals(pos.getErrorIndex(), 0); assertEquals(parsed, null); }
public void test_printText() { Random r = RandomFactory.getRandom(); int N = 8; Locale[] locales = Locale.getAvailableLocales(); Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); ZonedDateTime zdt = ZonedDateTime.now(); //System.out.printf("locale==%d, timezone=%d%n", locales.length, zids.size()); while (N-- > 0) { zdt = zdt.withDayOfYear(r.nextInt(365) + 1) .with(ChronoField.SECOND_OF_DAY, r.nextInt(86400)); for (String zid : zids) { if (zid.equals("ROC") || zid.startsWith("Etc/GMT")) { continue; // TBD: match jdk behavior? } zdt = zdt.withZoneSameLocal(ZoneId.of(zid)); TimeZone tz = TimeZone.getTimeZone(zid); boolean isDST = tz.inDaylightTime(new Date(zdt.toInstant().toEpochMilli())); for (Locale locale : locales) { String longDisplayName = tz.getDisplayName(isDST, TimeZone.LONG, locale); String shortDisplayName = tz.getDisplayName(isDST, TimeZone.SHORT, locale); if ((longDisplayName.startsWith("GMT+") && shortDisplayName.startsWith("GMT+")) || (longDisplayName.startsWith("GMT-") && shortDisplayName.startsWith("GMT-"))) { printText(locale, zdt, TextStyle.FULL, tz, tz.getID()); printText(locale, zdt, TextStyle.SHORT, tz, tz.getID()); continue; } printText(locale, zdt, TextStyle.FULL, tz, tz.getDisplayName(isDST, TimeZone.LONG, locale)); printText(locale, zdt, TextStyle.SHORT, tz, tz.getDisplayName(isDST, TimeZone.SHORT, locale)); } } } }
public void test_ParseText() { Locale[] locales = new Locale[] { Locale.ENGLISH, Locale.JAPANESE, Locale.FRENCH }; Set<String> zids = ZoneRulesProvider.getAvailableZoneIds(); for (Locale locale : locales) { parseText(zids, locale, TextStyle.FULL, false); parseText(zids, locale, TextStyle.FULL, true); parseText(zids, locale, TextStyle.SHORT, false); parseText(zids, locale, TextStyle.SHORT, true); } }
@Test(dataProvider="parseText") public void test_parse_strict_caseSensitive_parseLower(TemporalField field, TextStyle style, int value, String input) throws Exception { if (input.equals(input.toLowerCase(Locale.ROOT))) { // Skip if the given input is all lower case (e.g., "1st quarter") return; } setCaseSensitive(true); ParsePosition pos = new ParsePosition(0); getFormatter(field, style).parseUnresolved(input.toLowerCase(Locale.ROOT), pos); assertEquals(pos.getErrorIndex(), 0); }
@DataProvider(name="error") Object[][] data_error() { return new Object[][] { {DAY_OF_WEEK, TextStyle.FULL, "Monday", -1, IndexOutOfBoundsException.class}, {DAY_OF_WEEK, TextStyle.FULL, "Monday", 7, IndexOutOfBoundsException.class}, }; }
@Test(dataProvider="error") public void test_parse_error(TemporalField field, TextStyle style, String text, int pos, Class<?> expected) { try { getFormatter(field, style).parseUnresolved(text, new ParsePosition(pos)); } catch (RuntimeException ex) { assertTrue(expected.isInstance(ex)); } }
@Test(dataProvider="printText") public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception { if (style == TextStyle.FULL) { DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH); LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0); dt = dt.with(field, value); String text = f.format(dt); assertEquals(text, expected); } }
public void test_parse_noMatch_atEnd() throws Exception { ParsePosition pos = new ParsePosition(6); TemporalAccessor parsed = getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos); assertEquals(pos.getErrorIndex(), 6); assertEquals(parsed, null); }
@DataProvider(name="parseNumber") Object[][] provider_number() { return new Object[][] { {DAY_OF_MONTH, TextStyle.FULL, 1, "1"}, {DAY_OF_MONTH, TextStyle.FULL, 2, "2"}, {DAY_OF_MONTH, TextStyle.FULL, 30, "30"}, {DAY_OF_MONTH, TextStyle.FULL, 31, "31"}, {DAY_OF_MONTH, TextStyle.SHORT, 1, "1"}, {DAY_OF_MONTH, TextStyle.SHORT, 2, "2"}, {DAY_OF_MONTH, TextStyle.SHORT, 30, "30"}, {DAY_OF_MONTH, TextStyle.SHORT, 31, "31"}, }; }
@DataProvider(name="parseStandaloneText") Object[][] providerStandaloneText() { // Locale, TemporalField, TextStyle, expected value, input text return new Object[][] { {RUSSIAN, MONTH_OF_YEAR, TextStyle.FULL_STANDALONE, 1, "\u042f\u043d\u0432\u0430\u0440\u044c"}, {RUSSIAN, MONTH_OF_YEAR, TextStyle.FULL_STANDALONE, 12, "\u0414\u0435\u043a\u0430\u0431\u0440\u044c"}, {RUSSIAN, MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE, 1, "\u042f\u043d\u0432."}, {RUSSIAN, MONTH_OF_YEAR, TextStyle.SHORT_STANDALONE, 12, "\u0414\u0435\u043a."}, {FINNISH, DAY_OF_WEEK, TextStyle.FULL_STANDALONE, 2, "tiistai"}, {FINNISH, DAY_OF_WEEK, TextStyle.SHORT_STANDALONE, 2, "ti"}, }; }
public void test_parse_noMatch2() throws Exception { ParsePosition pos = new ParsePosition(3); TemporalAccessor parsed = getFormatter(DAY_OF_WEEK, TextStyle.FULL).parseUnresolved("Monday", pos); assertEquals(pos.getErrorIndex(), 3); assertEquals(parsed, null); }
@Test(dataProvider="parseText") public void test_parse_strict_caseSensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception { if (input.equals(input.toUpperCase(Locale.ROOT))) { // Skip if the given input is all upper case (e.g., "Q1") return; } setCaseSensitive(true); ParsePosition pos = new ParsePosition(0); getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos); assertEquals(pos.getErrorIndex(), 0); }
@Test(dataProvider="parseText") public void test_parse_strict_caseInsensitive_parseUpper(TemporalField field, TextStyle style, int value, String input) throws Exception { setCaseSensitive(false); ParsePosition pos = new ParsePosition(0); assertEquals(getFormatter(field, style).parseUnresolved(input.toUpperCase(Locale.ROOT), pos).getLong(field), (long) value); assertEquals(pos.getIndex(), input.length()); }