|
3 | 3 | import org.junit.Assert;
|
4 | 4 | import org.junit.Test;
|
5 | 5 |
|
| 6 | +import java.time.LocalDate; |
6 | 7 | import java.time.LocalDateTime;
|
| 8 | +import java.time.LocalTime; |
7 | 9 | import java.time.ZoneId;
|
8 | 10 | import java.time.ZonedDateTime;
|
9 | 11 | import java.time.format.DateTimeFormatter;
|
| 12 | +import java.time.format.DateTimeParseException; |
10 | 13 | import java.time.format.FormatStyle;
|
| 14 | +import java.time.temporal.ChronoUnit; |
11 | 15 | import java.util.Locale;
|
12 | 16 | import java.util.TimeZone;
|
13 | 17 |
|
@@ -43,4 +47,112 @@ public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingL
|
43 | 47 | Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
|
44 | 48 | Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
|
45 | 49 | }
|
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldPrintFormattedDate() { |
| 53 | + String europeanDatePattern = "dd.MM.yyyy"; |
| 54 | + DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern(europeanDatePattern); |
| 55 | + LocalDate summerDay = LocalDate.of(2016, 7, 31); |
| 56 | + Assert.assertEquals("31.07.2016", europeanDateFormatter.format(summerDay)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldPrintFormattedTime24() { |
| 61 | + String timeColonPattern = "HH:mm:ss"; |
| 62 | + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); |
| 63 | + LocalTime colonTime = LocalTime.of(17, 35, 50); |
| 64 | + Assert.assertEquals("17:35:50", timeColonFormatter.format(colonTime)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void shouldPrintFormattedTimeWithMillis() { |
| 69 | + String timeColonPattern = "HH:mm:ss SSS"; |
| 70 | + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); |
| 71 | + LocalTime colonTime = LocalTime.of(17, 35, 50).plus(329, ChronoUnit.MILLIS); |
| 72 | + Assert.assertEquals("17:35:50 329", timeColonFormatter.format(colonTime)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldPrintFormattedTimePM() { |
| 77 | + String timeColonPattern = "hh:mm:ss a"; |
| 78 | + DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern); |
| 79 | + LocalTime colonTime = LocalTime.of(17, 35, 50); |
| 80 | + Assert.assertEquals("05:35:50 PM", timeColonFormatter.format(colonTime)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void shouldPrintFormattedUTCRelatedZonedDateTime() { |
| 85 | + String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z"; |
| 86 | + DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern); |
| 87 | + LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15); |
| 88 | + Assert.assertEquals("31.07.2016 14:15 UTC-04:00", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("UTC-4")))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void shouldPrintFormattedNewYorkZonedDateTime() { |
| 93 | + String newYorkDateTimePattern = "dd.MM.yyyy HH:mm z"; |
| 94 | + DateTimeFormatter newYorkDateFormatter = DateTimeFormatter.ofPattern(newYorkDateTimePattern); |
| 95 | + LocalDateTime summerDay = LocalDateTime.of(2016, 7, 31, 14, 15); |
| 96 | + Assert.assertEquals("31.07.2016 14:15 EDT", newYorkDateFormatter.format(ZonedDateTime.of(summerDay, ZoneId.of("America/New_York")))); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void shouldPrintStyledDate() { |
| 101 | + LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23); |
| 102 | + Assert.assertEquals("Tuesday, August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(anotherSummerDay)); |
| 103 | + Assert.assertEquals("August 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(anotherSummerDay)); |
| 104 | + Assert.assertEquals("Aug 23, 2016", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(anotherSummerDay)); |
| 105 | + Assert.assertEquals("8/23/16", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(anotherSummerDay)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void shouldPrintStyledDateTime() { |
| 110 | + LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45); |
| 111 | + Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); |
| 112 | + Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); |
| 113 | + Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); |
| 114 | + Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void shouldPrintFormattedDateTimeWithPredefined() { |
| 119 | + Assert.assertEquals("2018-03-09", DateTimeFormatter.ISO_LOCAL_DATE.format(LocalDate.of(2018, 3, 9))); |
| 120 | + Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); |
| 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")))); |
| 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 | + } |
46 | 158 | }
|
0 commit comments