@Test public void testDayKeywords() { String todayTimestamp = "today"; String yesterdayTimestamp = "yesterday"; String tomorrowTimeStamp = "tomorrow"; Assert.assertEquals(DateTimeParser.parse(todayTimestamp), referenceDateTime()); Assert.assertEquals(DateTimeParser.parse(yesterdayTimestamp), referenceDateTime().minusDays(1)); Assert.assertEquals(DateTimeParser.parse(tomorrowTimeStamp), referenceDateTime().plusDays(1)); }
@Test public void testDateFormats() { int currentYear = referenceDateTime().getYear(); testFormat("12/30/14", new DateTime(2014, 12, 30, 0, 0, 0, 0)); testFormat("12/30/2014", new DateTime(2014, 12, 30, 0, 0, 0, 0)); testFormat("Jul 30", new DateTime(currentYear, 07, 30, 0, 0, 0, 0)); testFormat("Jul 30, 2013", new DateTime(2013, 07, 30, 0, 0, 0, 0)); testFormat("20141230", new DateTime(2014, 12, 30, 0, 0, 0, 0)); }
@Test public void testHourMinuteKeywords() { String noonTimestamp = "noon"; String teatimeTimestamp = "teatime"; String midnightTimestamp = "midnight"; Assert.assertEquals(DateTimeParser.parse(noonTimestamp), referenceDateTime().withHourOfDay(12).withMinuteOfHour(0)); Assert.assertEquals(DateTimeParser.parse(teatimeTimestamp), referenceDateTime().withHourOfDay(16).withMinuteOfHour(0)); Assert.assertEquals(DateTimeParser.parse(midnightTimestamp), referenceDateTime().withHourOfDay(0).withMinuteOfHour(0)); }
@Test public void testRegularHourMinute() { String hourMinuteTimestamp = "12:24"; String hourMinuteWithAm = "9:13am"; String hourMinuteWithPm = "09:13pm"; Assert.assertEquals(DateTimeParser.parse(hourMinuteTimestamp), referenceDateTime().withHourOfDay(12).withMinuteOfHour(24)); Assert.assertEquals(DateTimeParser.parse(hourMinuteWithAm), referenceDateTime().withHourOfDay(9).withMinuteOfHour(13)); Assert.assertEquals(DateTimeParser.parse(hourMinuteWithPm), referenceDateTime().withHourOfDay(21).withMinuteOfHour(13)); }
@Test public void testDayOfWeekFormat() { DateTime todayDate = referenceDateTime(); for (String dateTimeString: Arrays.asList("Fri", "14:42 Fri", "noon Fri")) { DateTime date = DateTimeParser.parse(dateTimeString); Assert.assertEquals(date.getDayOfWeek(), 5); Assert.assertTrue(todayDate.getDayOfYear() - date.getDayOfYear() <= 7); } }
@Test public void testComplexFormats() { testFormat("12:24 yesterday", nowDateTime().minusDays(1).withHourOfDay(12).withMinuteOfHour(24)); testFormat("12:24 tomorrow", nowDateTime().plusDays(1).withHourOfDay(12).withMinuteOfHour(24)); testFormat("12:24 today", nowDateTime().withHourOfDay(12).withMinuteOfHour(24)); testFormat("noon 12/30/2014", nowDateTime().withDate(2014, 12, 30).withHourOfDay(12).withMinuteOfHour(0)); int currentYear = referenceDateTime().getYear(); testFormat("15:45 12/30/14", new DateTime(2014, 12, 30, 15, 45, 0, 0)); testFormat("teatime 12/30/2014", new DateTime(2014, 12, 30, 16, 0, 0, 0)); testFormat("midnight Jul 30", new DateTime(currentYear, 07, 30, 0, 0, 0, 0)); testFormat("Jul 30, 2013", new DateTime(2013, 07, 30, 0, 0, 0, 0)); testFormat("Jul 30", new DateTime(currentYear, 07, 30, 0, 0, 0, 0)); testFormat("20141230", new DateTime(2014, 12, 30, 0, 0, 0, 0)); }