/** * 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()); }
/** * 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")); }
/** * 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)); } }