|
9 | 9 | import java.time.ZoneId; |
10 | 10 | import java.time.ZonedDateTime; |
11 | 11 | import java.time.format.DateTimeFormatter; |
| 12 | +import java.time.format.DateTimeParseException; |
12 | 13 | import java.time.format.FormatStyle; |
13 | 14 | import java.time.temporal.ChronoUnit; |
14 | 15 | import java.util.Locale; |
@@ -119,4 +120,39 @@ public void shouldPrintFormattedDateTimeWithPredefined() { |
119 | 120 | Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); |
120 | 121 | Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); |
121 | 122 | } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void shouldParseDateTime() { |
| 126 | + Assert.assertEquals(LocalDate.of(2018, 3, 12), LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse("2018-03-09")).plusDays(3)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void shouldParseFormatStyleFull() { |
| 131 | + ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).parse("Tuesday, August 23, 2016 1:12:45 PM EET")); |
| 132 | + Assert.assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 8, 23, 22, 12, 45), ZoneId.of("Europe/Bucharest")), dateTime.plusHours(9)); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void shouldParseDateWithCustomFormatter() { |
| 137 | + DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); |
| 138 | + Assert.assertFalse(LocalDate.from(europeanDateFormatter.parse("15.08.2014")).isLeapYear()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void shouldParseTimeWithCustomFormatter() { |
| 143 | + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); |
| 144 | + Assert.assertTrue(LocalTime.from(timeFormatter.parse("12:25:30 AM")).isBefore(LocalTime.NOON)); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void shouldParseZonedDateTimeWithCustomFormatter() { |
| 149 | + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); |
| 150 | + Assert.assertEquals(7200, ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15 GMT+02:00")).getOffset().getTotalSeconds()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test(expected = DateTimeParseException.class) |
| 154 | + public void shouldExpectAnExceptionIfDateTimeStringNotMatchPattern() { |
| 155 | + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); |
| 156 | + ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15")); |
| 157 | + } |
122 | 158 | } |
0 commit comments