/** * Parses the given date as an instant, assuming UTC timezone if unspecified. * * @param text the text to parse as an instant in UTC timezone by default, or {@code null}. * @return the instant for the given text, or {@code null} if the given text was null. * @throws DateTimeParseException if the text can not be parsed as a date. */ public static Instant parseInstantUTC(final CharSequence text) { return (text != null) ? parseInstantUTC(text, 0, text.length()) : null; }
/** * Parses the given unit symbol and set the {@link #epoch} if the parsed unit is a temporal unit. * This method is called by {@link #getUnit()}. */ @Override protected Unit<?> parseUnit(String symbols) { final Matcher parts = TIME_UNIT_PATTERN.matcher(symbols); if (parts.matches()) { /* * If we enter in this block, the unit is of the form "days since 1970-01-01 00:00:00". * The TIME_PATTERN splits the string in two parts, "days" and "1970-01-01 00:00:00". * The parse method will replace the space between date and time by 'T' letter. */ epoch = StandardDateFormat.parseInstantUTC(parts.group(2)); symbols = parts.group(1); } return Units.valueOf(symbols); }
/** * Tests parsing a date as an instant, assuming UTC timezone if unspecified. * * @since 1.0 */ @Test public void testParseInstant() { final long day = 1466985600000L; assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 )*1000), StandardDateFormat.parseInstantUTC("2016-06-27T16:48Z")); assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 + 12)*1000), StandardDateFormat.parseInstantUTC("2016-06-27T16:48:12Z")); assertEquals(Instant.ofEpochMilli(day + (( 3*60 + 2)*60 + 1)*1000 + 90), StandardDateFormat.parseInstantUTC("2016-06-27T03:02:01.09Z")); assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 + 12)*1000), StandardDateFormat.parseInstantUTC("2016-06-27T16:48:12")); assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 )*1000), StandardDateFormat.parseInstantUTC("2016-06-27T16:48")); assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 )*1000), StandardDateFormat.parseInstantUTC("2016-06-27 16:48")); assertEquals(Instant.ofEpochMilli(day), StandardDateFormat.parseInstantUTC("2016-06-27")); }