/** * Obtains an instance of {@code LocalTime} from a date-time object. * <p> * A {@code DateTimeAccessor} represents some form of date and time information. This factory converts the * arbitrary date-time object to an instance of {@code LocalTime}. * <p> * The conversion extracts the {@link ChronoField#NANO_OF_DAY nano-of-day} field. * * @param dateTime the date-time object to convert, not null * @return the local time, not null * @throws DateTimeException if unable to convert to a {@code LocalTime} */ public static LocalTime from(DateTimeAccessor dateTime) { if (dateTime instanceof LocalTime) { return (LocalTime) dateTime; } else if (dateTime instanceof ChronoLocalDateTime) { return ((ChronoLocalDateTime<?>) dateTime).getTime(); } else if (dateTime instanceof ZonedDateTime) { return ((ChronoZonedDateTime<?>) dateTime).getTime(); } // handle builder as a special case if (dateTime instanceof DateTimeBuilder) { DateTimeBuilder builder = (DateTimeBuilder) dateTime; LocalTime time = builder.extract(LocalTime.class); if (time != null) { return time; } } return ofNanoOfDay(dateTime.getLong(NANO_OF_DAY)); }
/** * Obtains an instance of {@code LocalDate} from a date-time object. * <p> * A {@code DateTimeAccessor} represents some form of date and time information. This factory converts the * arbitrary date-time object to an instance of {@code LocalDate}. * <p> * The conversion extracts the {@link ChronoField#EPOCH_DAY epoch-day} field. * * @param dateTime the date-time object to convert, not null * @return the local date, not null * @throws DateTimeException if unable to convert to a {@code LocalDate} */ public static LocalDate from(DateTimeAccessor dateTime) { if (dateTime instanceof LocalDate) { return (LocalDate) dateTime; } else if (dateTime instanceof LocalDateTime) { return ((LocalDateTime) dateTime).getDate(); } else if (dateTime instanceof ZonedDateTime) { return ((ZonedDateTime) dateTime).getDate(); } // handle builder as a special case if (dateTime instanceof DateTimeBuilder) { DateTimeBuilder builder = (DateTimeBuilder) dateTime; LocalDate date = builder.extract(LocalDate.class); if (date != null) { return date; } } return ofEpochDay(dateTime.getLong(EPOCH_DAY)); }
/** * Parses the text to a builder. * <p> * This parses to a {@code DateTimeBuilder} ensuring that the text is fully parsed. This method throws * {@link DateTimeParseException} if unable to parse, or some other {@code DateTimeException} if another * date/time problem occurs. * * @param text the text to parse, not null * @return the engine representing the result of the parse, not null * @throws DateTimeParseException if the parse fails * @throws DateTimeException if there is a date/time problem */ public DateTimeBuilder parseToBuilder(CharSequence text) { Jdk7Methods.Objects_requireNonNull(text, "text"); String str = text.toString(); // parsing whole String, so this makes sense ParsePosition pos = new ParsePosition(0); DateTimeBuilder result = parseToBuilder(str, pos); if (result == null || pos.getErrorIndex() >= 0 || pos.getIndex() < str.length()) { String abbr = str.toString(); if (abbr.length() > 64) { abbr = abbr.substring(0, 64) + "..."; } if (pos.getErrorIndex() >= 0) { throw new DateTimeParseException("Text '" + abbr + "' could not be parsed at index " + pos.getErrorIndex(), str, pos.getErrorIndex()); } else { throw new DateTimeParseException("Text '" + abbr + "' could not be parsed, unparsed text found at index " + pos.getIndex(), str, pos.getIndex()); } } return result; }
private int parseSecs(String str) { if (str.equals("-")) { return 0; } int pos = 0; if (str.startsWith("-")) { pos = 1; } ParsePosition pp = new ParsePosition(pos); DateTimeBuilder bld = TIME_PARSER.parseToBuilder(str, pp); if (bld == null || pp.getErrorIndex() >= 0) { throw new IllegalArgumentException(str); } Map<DateTimeField, Long> parsed = bld.getFieldValueMap(); long hour = parsed.get(HOUR_OF_DAY); Long min = parsed.get(MINUTE_OF_HOUR); Long sec = parsed.get(SECOND_OF_MINUTE); int secs = (int) (hour * 60 * 60 + (min != null ? min : 0) * 60 + (sec != null ? sec : 0)); if (pos == 1) { secs = -secs; } return secs; }
private DateTimeBuilder createTime(Integer hour, Integer min, Integer sec, Integer nano) { DateTimeBuilder test = new DateTimeBuilder(); if (hour != null) { test.addFieldValue(HOUR_OF_DAY, hour); } if (min != null) { test.addFieldValue(MINUTE_OF_HOUR, min); } if (sec != null) { test.addFieldValue(SECOND_OF_MINUTE, sec); } if (nano != null) { test.addFieldValue(NANO_OF_SECOND, nano); } return test; }
@Test(dataProvider = "combine") public void test_derive(DateTimeField field1, Number value1, DateTimeField field2, Number value2, DateTimeField field3, Number value3, DateTimeField field4, Number value4, Class<?> query, Object expectedVal) { DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue()); if (field2 != null) { builder.addFieldValue(field2, value2.longValue()); } if (field3 != null) { builder.addFieldValue(field3, value3.longValue()); } if (field4 != null) { builder.addFieldValue(field4, value4.longValue()); } builder.resolve(); assertEquals(builder.extract((Class<?>) query), expectedVal); }
@Test(dataProvider = "normalized") public void test_normalized(DateTimeField field1, Number value1, DateTimeField field2, Number value2, DateTimeField field3, Number value3, DateTimeField query, Number expectedVal) { DateTimeBuilder builder = new DateTimeBuilder(field1, value1.longValue()); if (field2 != null) { builder.addFieldValue(field2, value2.longValue()); } if (field3 != null) { builder.addFieldValue(field3, value3.longValue()); } builder.resolve(); if (expectedVal != null) { assertEquals(builder.getLong(query), expectedVal.longValue()); } else { assertEquals(builder.containsFieldValue(query), false); } }
public void test_split() { DateTimeBuilder builder = new DateTimeBuilder(); builder.addCalendrical(LocalDateTime.of(2012, 6, 30, 12, 30)); builder.addCalendrical(ZoneOffset.ofHours(2)); builder.resolve(); assertEquals(builder.build(LocalDate.class), LocalDate.of(2012, 6, 30)); assertEquals(builder.build(LocalTime.class), LocalTime.of(12, 30)); assertEquals(builder.build(ZoneOffset.class), ZoneOffset.ofHours(2)); assertEquals(builder.build(LocalDateTime.class), LocalDateTime.of(2012, 6, 30, 12, 30)); assertEquals(builder.build(OffsetDate.class), OffsetDate.of(2012, 6, 30, ZoneOffset.ofHours(2))); assertEquals(builder.build(OffsetTime.class), OffsetTime.of(12, 30, ZoneOffset.ofHours(2))); // assertEquals(builder.build(OffsetDateTime.class), OffsetDateTime.of(2012, 6, 30, 12, 30, // ZoneOffset.ofHours(2))); }
/** * Returns a {@code DateTimeBuilder} that can be used to interpret the results of the parse. * <p> * This method is typically used once parsing is complete to obtain the parsed data. Parsing will typically * result in separate fields, such as year, month and day. The returned builder can be used to combine the * parsed data into meaningful objects such as {@code LocalDate}, potentially applying complex processing to * handle invalid parsed data. * * @return a new builder with the results of the parse, not null */ public DateTimeBuilder toBuilder() { List<Object> cals = currentParsed().parsed; DateTimeBuilder builder = new DateTimeBuilder(); for (Object obj : cals) { if (obj instanceof FieldValue) { FieldValue fv = (FieldValue) obj; builder.addFieldValue(fv.field, fv.value); } else { builder.addCalendrical(obj); } } return builder; }
@Test(groups = { "tck" }) public void test_toBuilder() throws Exception { this.context.setParsedField(YEAR, 2008); this.context.setParsedField(MONTH_OF_YEAR, 6); DateTimeBuilder builder = this.context.toBuilder(); Map<DateTimeField, Long> fields = builder.getFieldValueMap(); assertEquals(fields.size(), 2); assertEquals(fields.get(YEAR), Long.valueOf(2008)); assertEquals(fields.get(MONTH_OF_YEAR), Long.valueOf(6)); }
@Test(dataProvider = "offsets") public void test_print(String pattern, String expected, ZoneOffset offset) throws Exception { this.buf.append("EXISTING"); this.printContext.setDateTime(new DateTimeBuilder(OFFSET_SECONDS, offset.getTotalSeconds())); ZoneOffsetPrinterParser pp = new ZoneOffsetPrinterParser("NO-OFFSET", pattern); pp.print(this.printContext, this.buf); assertEquals(this.buf.toString(), "EXISTING" + expected); }
public void test_print_emptyAppendable() throws Exception { this.printContext.setDateTime(new DateTimeBuilder(OFFSET_SECONDS, OFFSET_0130.getTotalSeconds())); ZoneOffsetPrinterParser pp = new ZoneOffsetPrinterParser("Z", "+HH:MM:ss"); pp.print(this.printContext, this.buf); assertEquals(this.buf.toString(), "+01:30"); }
private static void resolve1() { DateTimeBuilder builder = new DateTimeBuilder(); builder.addFieldValue(ChronoField.YEAR, 2012); builder.addFieldValue(ChronoField.MONTH_OF_YEAR, 4); builder.addFieldValue(ChronoField.DAY_OF_MONTH, 18); System.out.println("Setup: " + builder); System.out.println("Resolved: " + builder.resolve()); System.out.println("Date: " + LocalDate.from(builder)); }
private static void resolve2() { DateTimeBuilder builder = new DateTimeBuilder(); builder.addFieldValue(ChronoField.YEAR, 2012); builder.addFieldValue(ChronoField.MONTH_OF_YEAR, 4); builder.addFieldValue(ChronoField.DAY_OF_MONTH, 18); builder.addFieldValue(ChronoField.DAY_OF_WEEK, 1); System.out.println("Setup: " + builder); try { builder.resolve(); } catch (RuntimeException ex) { System.err.println(ex.toString()); } }
private static void resolve3() { DateTimeBuilder builder = new DateTimeBuilder(); builder.addCalendrical(LocalDate.of(2012, 1, 2)); builder.addCalendrical(ZonedDateTime.of(LocalDateTime.of(2012, 4, 3, 12, 30), ZoneOffset.ofHours(2))); System.out.println("Setup: " + builder); try { builder.resolve(); } catch (RuntimeException ex) { System.err.println(ex.toString()); } }
private static void resolve4() { DateTimeBuilder builder = new DateTimeBuilder(); builder.addFieldValue(ChronoField.YEAR, 2012); builder.addFieldValue(ChronoField.MONTH_OF_YEAR, 5); builder.addFieldValue(ChronoField.DAY_OF_MONTH, 3); System.out.println("Setup: " + builder); System.out.println("Resolved: " + builder.resolve()); System.out.println("Date: " + LocalDate.from(builder)); }
@Test(dataProvider = "sample_isoLocalDate", groups = { "tck" }) public void test_parse_isoLocalDate(Integer year, Integer month, Integer day, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDate(year, month, day); // offset/zone not expected to be parsed assertParseMatch(DateTimeFormatters.isoLocalDate().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(groups = { "tck" }) public void test_parse_isoLocalDate_999999999() { DateTimeBuilder expected = createDate(999999999, 8, 6); assertParseMatch(DateTimeFormatters.isoLocalDate().parseToBuilder("+999999999-08-06", new ParsePosition(0)), expected); assertEquals(LocalDate.parse("+999999999-08-06"), LocalDate.of(999999999, 8, 6)); }
@Test(groups = { "tck" }) public void test_parse_isoLocalDate_1000000000() { DateTimeBuilder expected = createDate(1000000000, 8, 6); assertParseMatch(DateTimeFormatters.isoLocalDate().parseToBuilder("+1000000000-08-06", new ParsePosition(0)), expected); }
@Test(groups = { "tck" }) public void test_parse_isoLocalDate_M999999999() { DateTimeBuilder expected = createDate(-999999999, 8, 6); assertParseMatch(DateTimeFormatters.isoLocalDate().parseToBuilder("-999999999-08-06", new ParsePosition(0)), expected); assertEquals(LocalDate.parse("-999999999-08-06"), LocalDate.of(-999999999, 8, 6)); }
@Test(groups = { "tck" }) public void test_parse_isoLocalDate_M1000000000() { DateTimeBuilder expected = createDate(-1000000000, 8, 6); assertParseMatch(DateTimeFormatters.isoLocalDate().parseToBuilder("-1000000000-08-06", new ParsePosition(0)), expected); }
@Test(dataProvider = "sample_isoOffsetDate", groups = { "tck" }) public void test_parse_isoOffsetDate(Integer year, Integer month, Integer day, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDate(year, month, day); buildCalendrical(expected, offsetId, null); // zone not expected to be parsed assertParseMatch(DateTimeFormatters.isoOffsetDate().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoDate", groups = { "tck" }) public void test_parse_isoDate(Integer year, Integer month, Integer day, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDate(year, month, day); if (offsetId != null) { expected.addFieldValue(OFFSET_SECONDS, ZoneOffset.of(offsetId).getTotalSeconds()); } assertParseMatch(DateTimeFormatters.isoDate().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoLocalTime", groups = { "tck" }) public void test_parse_isoLocalTime(Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createTime(hour, min, sec, nano); // offset/zone not expected to be parsed assertParseMatch(DateTimeFormatters.isoLocalTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoOffsetTime", groups = { "tck" }) public void test_parse_isoOffsetTime(Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createTime(hour, min, sec, nano); buildCalendrical(expected, offsetId, null); // zoneId is not expected from parse assertParseMatch(DateTimeFormatters.isoOffsetTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoTime", groups = { "tck" }) public void test_parse_isoTime(Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createTime(hour, min, sec, nano); if (offsetId != null) { expected.addFieldValue(OFFSET_SECONDS, ZoneOffset.of(offsetId).getTotalSeconds()); } assertParseMatch(DateTimeFormatters.isoTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoLocalDateTime", groups = { "tck" }) public void test_parse_isoLocalDateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDateTime(year, month, day, hour, min, sec, nano); assertParseMatch(DateTimeFormatters.isoLocalDateTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoOffsetDateTime", groups = { "tck" }) public void test_parse_isoOffsetDateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDateTime(year, month, day, hour, min, sec, nano); buildCalendrical(expected, offsetId, null); // zone not expected to be parsed assertParseMatch(DateTimeFormatters.isoOffsetDateTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoZonedDateTime", groups = { "tck" }) public void test_parse_isoZonedDateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDateTime(year, month, day, hour, min, sec, nano); if (offsetId.equals(zoneId)) { buildCalendrical(expected, offsetId, null); } else { buildCalendrical(expected, offsetId, zoneId); } assertParseMatch(DateTimeFormatters.isoZonedDateTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(dataProvider = "sample_isoDateTime", groups = { "tck" }) public void test_parse_isoDateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano, String offsetId, String zoneId, String input, Class<?> invalid) { if (input != null) { DateTimeBuilder expected = createDateTime(year, month, day, hour, min, sec, nano); if (offsetId != null) { expected.addFieldValue(OFFSET_SECONDS, ZoneOffset.of(offsetId).getTotalSeconds()); if (zoneId != null) { expected.addCalendrical(ZoneId.of(zoneId)); } } assertParseMatch(DateTimeFormatters.isoDateTime().parseToBuilder(input, new ParsePosition(0)), expected); } }
@Test(groups = { "tck" }) public void test_parse_weekDate_largeYear() { DateTimeBuilder builder = DateTimeFormatters.isoWeekDate().parseToBuilder("+123456-W04-5", new ParsePosition(0)); assertEquals(builder.getFieldValue(ISOWeeks.WEEK_BASED_YEAR), 123456); assertEquals(builder.getFieldValue(ISOWeeks.WEEK_OF_WEEK_BASED_YEAR), 4); assertEquals(builder.getFieldValue(DAY_OF_WEEK), 5); }
private DateTimeBuilder createDate(Integer year, Integer month, Integer day) { DateTimeBuilder test = new DateTimeBuilder(); if (year != null) { test.addFieldValue(YEAR, year); } if (month != null) { test.addFieldValue(MONTH_OF_YEAR, month); } if (day != null) { test.addFieldValue(DAY_OF_MONTH, day); } return test; }
private DateTimeBuilder createDateTime(Integer year, Integer month, Integer day, Integer hour, Integer min, Integer sec, Integer nano) { DateTimeBuilder test = new DateTimeBuilder(); if (year != null) { test.addFieldValue(YEAR, year); } if (month != null) { test.addFieldValue(MONTH_OF_YEAR, month); } if (day != null) { test.addFieldValue(DAY_OF_MONTH, day); } if (hour != null) { test.addFieldValue(HOUR_OF_DAY, hour); } if (min != null) { test.addFieldValue(MINUTE_OF_HOUR, min); } if (sec != null) { test.addFieldValue(SECOND_OF_MINUTE, sec); } if (nano != null) { test.addFieldValue(NANO_OF_SECOND, nano); } return test; }
private void buildCalendrical(DateTimeBuilder cal, String offsetId, String zoneId) { if (offsetId != null) { cal.addFieldValue(OFFSET_SECONDS, ZoneOffset.of(offsetId).getTotalSeconds()); } if (zoneId != null) { cal.addCalendrical(ZoneId.of(zoneId)); } }
private void assertParseMatch(DateTimeBuilder parsed, DateTimeBuilder expected) { Map<DateTimeField, Long> parsedFVMap = parsed.getFieldValueMap(); Map<DateTimeField, Long> expectedFVMap = expected.getFieldValueMap(); assertEquals(parsedFVMap, expectedFVMap); List<Object> parsedCMap = parsed.getCalendricalList(); List<Object> expectedCMap = expected.getCalendricalList(); assertEquals(parsedCMap, expectedCMap); }
@Test(groups = { "tck" }) public void test_parseToBuilder_String() throws Exception { DateTimeFormatter test = this.fmt.withLocale(Locale.ENGLISH).withSymbols(DateTimeFormatSymbols.STANDARD); DateTimeBuilder result = test.parseToBuilder("ONE30"); assertEquals(result.getFieldValueMap().size(), 1); assertEquals(result.getFieldValue(DAY_OF_MONTH), 30L); assertEquals(result.getCalendricalList().size(), 0); }