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