/** * Returns a clone of this format. * * @return a clone of this format. */ @Override @SuppressWarnings("CloneDoesntCallSuperClone") public Object clone() { final StandardDateFormat clone = new StandardDateFormat(); clone.format = format; return clone; } }
/** * Returns the current value of {@link XMLStreamReader#getElementText()} as a date, * or {@code null} if that value is null or empty. * * @return the current text element as a date, or {@code null} if empty. * @throws XMLStreamException if a text element can not be returned. * @throws DateTimeParseException if the text can not be parsed as a date. */ protected final Date getElementAsDate() throws XMLStreamException { final String text = getElementText(); return (text != null) ? StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse(text)) : null; }
/** * 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 text starting at the given position. * Contrarily to {@link #parse(String)}, this method does not accept spaces as a separator between date and time. * * @param text the text to parse. * @param position position where to start the parsing. * @return the date, or {@code null} if we failed to parse it. */ @Override public Date parse(final String text, final ParsePosition position) { try { return toDate(format.parse(text, position)); } catch (DateTimeException | ArithmeticException e) { position.setErrorIndex(getErrorIndex(e, position)); return null; } }
/** * Parses the given text. This method accepts space as a separator between date and time. * * @param text the text to parse. * @return the date (never null). * @throws ParseException if the parsing failed. */ @Override public Date parse(final String text) throws ParseException { try { return toDate(format.parse(toISO(text, 0, text.length()))); } catch (DateTimeException | ArithmeticException e) { throw (ParseException) new ParseException(e.getLocalizedMessage(), getErrorIndex(e, null)).initCause(e); } }
/** * Parses the given text starting at the given position. * * @param text the text to parse. * @param position position where to start the parsing. * @return the date, or {@code null} if we failed to parse it. */ @Override public Date parse(final String text, final ParsePosition position) { if (isUserSpecifiedPattern) { return super.parse(text, position); } final int fromIndex = position.getIndex(); final String modified = dateToISO(text, fromIndex, false); position.setIndex(0); final Date date = super.parse(modified, position); position.setIndex (adjustIndex(text, modified, fromIndex, position.getIndex())); position.setErrorIndex(adjustIndex(text, modified, fromIndex, position.getErrorIndex())); return date; }
/** * Tests formatting and parsing a negative year. * This test uses the Julian epoch (January 1st, 4713 BC at 12:00 UTC in proleptic Julian calendar; * equivalent to November 24, 4714 BC when expressed in the proleptic Gregorian calendar instead). * We use astronomical year numbering: 4714 BC is numbered -4713. * * @throws ParseException if an error occurred while parsing the date. */ @Test public void testNegativeYear() throws ParseException { final Date julian = new Date(-210866760000000L); // Same epoch than CommonCRS.Temporal.JULIAN. final String expected = "-4713-11-24T12:00:00.000"; // Proleptic Gregorian calendar, astronomical year. final StandardDateFormat f = new StandardDateFormat(); assertEquals(expected, f.format(julian)); assertEquals(julian, f.parse(expected)); } }
/** * Tests parsing a date. * * @throws ParseException if an error occurred while parsing the date. */ @Test public void testParse() throws ParseException { final long day = 1466985600000L; final StandardDateFormat f = new StandardDateFormat(); assertEquals("millis", day + ((16*60 + 48)*60 )*1000, f.parse("2016-06-27T16:48Z") .getTime()); assertEquals("millis", day + ((16*60 + 48)*60 + 12)*1000, f.parse("2016-06-27T16:48:12Z") .getTime()); assertEquals("millis", day, f.parse("2016-06-27") .getTime()); assertEquals("millis", day + (( 3*60 + 2)*60 + 1)*1000 + 90, f.parse("2016-06-27T03:02:01.09Z").getTime()); assertEquals(date("2009-01-01 05:00:00"), f.parse("2009-01-01T06:00:00+01:00")); assertEquals(date("2005-09-22 04:30:15"), f.parse("2005-09-22T04:30:15Z")); assertEquals(date("2005-09-22 04:30:15"), f.parse("2005-09-22T04:30:15")); assertEquals(date("2005-09-22 04:30:00"), f.parse("2005-09-22T04:30")); assertEquals(date("2005-09-22 04:30:00"), f.parse("2005-09-22 04:30")); assertEquals(date("2005-09-22 04:00:00"), f.parse("2005-09-22T04")); assertEquals(date("2005-09-22 00:00:00"), f.parse("2005-09-22")); assertEquals(date("2005-09-22 00:00:00"), f.parse("2005-9-22")); assertEquals(date("1992-01-01 00:00:00"), f.parse("1992-1-1")); }
/** * Parses a text string like {@code 2007-12-03T10:15:30+01:00}. * * @param text the text to parse. * @return the parsed date. * @throws DateTimeParseException if the text cannot be parsed. */ public static OffsetDateTime parse(final CharSequence text) { final String modified = StandardDateFormat.dateToISO(text, 0, false); final Date date; try { synchronized (FORMAT) { date = FORMAT.parse(modified); } } catch (ParseException e) { throw new DateTimeParseException(e.getLocalizedMessage()); } return new OffsetDateTime(date.getTime()); }
/** * Parses the given date and/or time, which may have an optional timezone. This method applies heuristic rules * for choosing if the object should be returned as a local date, or a date and time with timezone, <i>etc</i>. * The full date format is of the form "1970-01-01T00:00:00.000Z", but this method also accepts spaces in place * of 'T' as in "1970-01-01 00:00:00". * * @param text the character string to parse, or {@code null}. * @return a temporal object 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. * * @since 0.8 */ public static Temporal parseBest(final CharSequence text) { // Cast is safe if all QUERIES elements return a Temporal subtype. return (text != null) ? (Temporal) FORMAT.parseBest(toISO(text, 0, text.length()), QUERIES) : null; }
/** * Returns the current value of {@link XMLStreamReader#getElementText()} as a temporal object, * or {@code null} if that value is null or empty. * * @return the current text element as a temporal object, or {@code null} if empty. * @throws XMLStreamException if a text element can not be returned. * @throws DateTimeParseException if the text can not be parsed as a date. */ protected final Temporal getElementAsTemporal() throws XMLStreamException { return StandardDateFormat.parseBest(getElementText()); }
/** * Parses the given text. * * @param text the text to parse. * @return the instant. * @throws DateTimeException if the text can not be parsed. */ public static Instant parse(final CharSequence text) { final Date time; try { synchronized (parser) { time = parser.parse(text.toString()); } } catch (ParseException e) { throw new DateTimeException(e.getMessage()); } return new Instant(time.getTime()); }
/** * Parses a text string like {@code 10:15:30+01:00}. * * @param text the text to parse. * @return the parsed date. * @throws DateTimeParseException if the text cannot be parsed. */ public static OffsetTime parse(final CharSequence text) { final String modified = StandardDateFormat.dateToISO(text, 0, true); final Date date; try { synchronized (FORMAT) { date = FORMAT.parse(modified); } } catch (ParseException e) { throw new DateTimeParseException(e.getLocalizedMessage()); } return new OffsetTime(date.getTime()); } }
/** * Helper method for {@link #testToISO()}. */ private static String toISO(final String text) { return StandardDateFormat.toISO(text, 0, text.length()).toString(); }
/** * Tests parsing a temporal object. * * @since 0.8 */ @Test public void testParseBest() { final long day = 1466985600000L; assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 )*1000), StandardDateFormat.parseBest("2016-06-27T16:48Z")); assertEquals(Instant.ofEpochMilli(day + ((16*60 + 48)*60 + 12)*1000), StandardDateFormat.parseBest("2016-06-27T16:48:12Z")); assertEquals(Instant.ofEpochMilli(day + (( 3*60 + 2)*60 + 1)*1000 + 90), StandardDateFormat.parseBest("2016-06-27T03:02:01.09Z")); assertEquals(LocalDateTime.of(2016, 6, 27, 16, 48, 12), StandardDateFormat.parseBest("2016-06-27T16:48:12")); assertEquals(LocalDateTime.of(2016, 6, 27, 16, 48), StandardDateFormat.parseBest("2016-06-27T16:48")); assertEquals(LocalDateTime.of(2016, 6, 27, 16, 48), StandardDateFormat.parseBest("2016-06-27 16:48")); assertEquals(LocalDate.of(2016, 6, 27), StandardDateFormat.parseBest("2016-06-27")); }
/** * Parses the date at the given position. * This is a helper method for {@link Element} only. * * <p>The WKT 2 format expects dates formatted according the ISO 9075-2 standard.</p> */ final Date parseDate(final String text, final ParsePosition position) { if (dateFormat == null) { dateFormat = new StandardDateFormat(symbols.getLocale()); } return dateFormat.parse(text, position); }
/** * Returns the value of the attribute of the given name as a date, or {@code null} if none. * If there is more than one numeric value, only the first one is returned. * * @return {@inheritDoc} */ @Override public Date dateValue(final String name) { final Object value = findAttribute(name); if (value instanceof CharSequence) try { return StandardDateFormat.toDate(StandardDateFormat.FORMAT.parse((CharSequence) value)); } catch (DateTimeException | ArithmeticException e) { listeners.warning(null, e); } return 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); }
/** * Converts the given numerical values to date, using the information provided in the given unit symbol. * The unit symbol is typically a string like <cite>"days since 1970-01-01T00:00:00Z"</cite>. * * @param values the values to convert. May contain {@code null} elements. * @return the converted values. May contain {@code null} elements. */ @Override public Date[] numberToDate(final String symbol, final Number... values) { final Date[] dates = new Date[values.length]; final String[] parts = TIME_UNIT_PATTERN.split(symbol); if (parts.length == 2) try { final UnitConverter converter = Units.valueOf(parts[0]).getConverterToAny(Units.MILLISECOND); final long epoch = JDK8.parseDateTime(StandardDateFormat.dateToISO(parts[1], 0, false)).getTime(); for (int i=0; i<values.length; i++) { final Number value = values[i]; if (value != null) { dates[i] = new Date(epoch + Math.round(converter.convert(value.doubleValue()))); } } } catch (IncommensurableException | ParserException | DateTimeException | ArithmeticException e) { listeners.warning(null, e); } return dates; }
/** * 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. * @param lower index of the first character to parse. * @param upper index after the last character to parse. * @return the instant for the given text. * @throws DateTimeParseException if the text can not be parsed as a date. */ public static Instant parseInstantUTC(final CharSequence text, final int lower, final int upper) { TemporalAccessor date = FORMAT.parseBest(toISO(text, lower, upper), QUERIES); if (date instanceof Instant) { return (Instant) date; } final OffsetDateTime time; if (date instanceof LocalDateTime) { time = ((LocalDateTime) date).atOffset(ZoneOffset.UTC); } else { time = ((LocalDate) date).atTime(MIDNIGHT); } return time.toInstant(); }