Skip to content

Commit f9065d2

Browse files
committed
BAEL-2258 Guide to DateTimeFormatter. Tests added for various formatting patterns and styles
1 parent 5252c5f commit f9065d2

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

‎core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6+
import java.time.LocalDate;
67
import java.time.LocalDateTime;
8+
import java.time.LocalTime;
79
import java.time.ZoneId;
810
import java.time.ZonedDateTime;
911
import java.time.format.DateTimeFormatter;
1012
import java.time.format.FormatStyle;
13+
import java.time.temporal.ChronoUnit;
1114
import java.util.Locale;
1215
import java.util.TimeZone;
1316

@@ -43,4 +46,70 @@ public void givenDateTimeAndTimeZone_whenFormatWithDifferentLocales_thenGettingL
4346
Assert.assertEquals("Monday, January 1, 2018 10:15:50 AM PST", formattedDateTime);
4447
Assert.assertEquals("lundi 1 janvier 2018 10 h 15 PST", frFormattedDateTime);
4548
}
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+
}
46115
}

0 commit comments

Comments
 (0)