Skip to content

Commit 3bac343

Browse files
authored
BAEL-7545: Fix DateTimeParseException (#18433)
1 parent a2aa699 commit 3bac343

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.baeldung.datetimeparseexception;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.*;
6+
import java.time.format.DateTimeFormatter;
7+
import java.time.temporal.TemporalAccessor;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
public class DateTimeParseExceptionUnitTest {
12+
13+
// Failing Test: Trying to obtain LocalDateTime from a date without time
14+
@Test
15+
void givenDateWithoutTime_whenParsedAsLocalDateTime_thenThrowsDateTimeParseException() {
16+
String dateStr = "2024-03-25";
17+
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
18+
TemporalAccessor parsedDate = formatter.parse(dateStr);
19+
20+
DateTimeException exception = assertThrows(DateTimeException.class, () ->
21+
LocalDateTime.from(parsedDate)
22+
);
23+
24+
assertTrue(exception.getMessage().contains("Unable to obtain LocalDateTime from TemporalAccessor"));
25+
}
26+
27+
// Fixed Version: Correctly using LocalDate
28+
@Test
29+
void givenDateWithoutTime_whenParsedAsLocalDate_thenSucceeds() {
30+
String dateStr = "2024-03-25";
31+
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
32+
LocalDate date = LocalDate.parse(dateStr, formatter);
33+
34+
assertEquals(LocalDate.of(2024, 3, 25), date);
35+
}
36+
37+
// Fixed Version: Correctly using LocalDateTime with default time
38+
@Test
39+
void givenDateWithTime_whenParsedAsLocalDateTime_thenSucceeds() {
40+
String dateTimeStr = "2024-03-25T00:00:00";
41+
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
42+
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);
43+
44+
assertEquals(LocalDateTime.of(2024, 3, 25, 0, 0, 0), dateTime);
45+
}
46+
47+
// Failing Test: Trying to obtain LocalDateTime from DayOfWeek
48+
@Test
49+
void givenDayOfWeek_whenParsedAsLocalDateTime_thenThrowsDateTimeParseException() {
50+
TemporalAccessor parsedDate = DayOfWeek.FRIDAY;
51+
52+
DateTimeException exception = assertThrows(DateTimeException.class, () ->
53+
LocalDateTime.from(parsedDate)
54+
);
55+
56+
assertTrue(exception.getMessage().contains("Unable to obtain LocalDateTime from TemporalAccessor"));
57+
}
58+
59+
// Fixed Version: Correctly using DayOfWeek
60+
@Test
61+
void givenDayOfWeek_whenUsedAsIs_thenSucceeds() {
62+
DayOfWeek day = DayOfWeek.FRIDAY;
63+
64+
assertEquals(DayOfWeek.FRIDAY, day);
65+
}
66+
67+
// Fixed Version: Combine DayOfWeek with LocalDate and LocalTime
68+
@Test
69+
void givenDayOfWeek_whenCombinedWithLocalDateAndLocalTime_thenSucceeds() {
70+
LocalDate date = LocalDate.of(2024, 3, 25); // Specific date
71+
LocalTime time = LocalTime.of(14, 30); // Set a specific time
72+
LocalDateTime dateTime = LocalDateTime.of(date, time);
73+
74+
assertEquals(LocalDateTime.of(2024, 3, 25, 14, 30), dateTime);
75+
}
76+
77+
// Failing Test: Trying to obtain LocalDateTime from LocalTime
78+
@Test
79+
void givenLocalTime_whenParsedAsLocalDateTime_thenThrowsDateTimeParseException() {
80+
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
81+
TemporalAccessor parsedDate = formatter.parse("14:30:00");
82+
83+
DateTimeException exception = assertThrows(DateTimeException.class, () ->
84+
LocalDateTime.from(parsedDate)
85+
);
86+
87+
assertTrue(exception.getMessage().contains("Unable to obtain LocalDateTime from TemporalAccessor"));
88+
}
89+
90+
// Fixed Version: Combine LocalTime with LocalDate to form a valid LocalDateTime
91+
@Test
92+
void givenLocalTime_whenCombinedWithLocalDate_thenSucceeds() {
93+
LocalDate date = LocalDate.of(2024, 3, 25);
94+
LocalTime time = LocalTime.parse("14:30:00");
95+
LocalDateTime dateTime = LocalDateTime.of(date, time);
96+
97+
assertNotNull(dateTime);
98+
assertEquals(LocalDateTime.of(2024, 3, 25, 14, 30), dateTime);
99+
}
100+
101+
// Failing Test: Trying to obtain LocalDateTime from YearMonth
102+
@Test
103+
void givenYearMonth_whenParsedAsLocalDateTime_thenThrowsDateTimeParseException() {
104+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
105+
TemporalAccessor parsedDate = formatter.parse("2024-03");
106+
107+
DateTimeException exception = assertThrows(DateTimeException.class, () ->
108+
LocalDateTime.from(parsedDate)
109+
);
110+
111+
assertTrue(exception.getMessage().contains("Unable to obtain LocalDateTime from TemporalAccessor"));
112+
}
113+
114+
// Fixed Version: Correctly using YearMonth
115+
@Test
116+
void givenYearMonth_whenParsedCorrectly_thenSucceeds() {
117+
YearMonth yearMonth = YearMonth.parse("2024-03", DateTimeFormatter.ofPattern("yyyy-MM"));
118+
assertEquals(YearMonth.of(2024, 3), yearMonth);
119+
}
120+
121+
// Failing Test: Trying to obtain LocalDateTime from MonthDay
122+
@Test
123+
void givenMonthDay_whenParsedAsLocalDateTime_thenThrowsDateTimeParseException() {
124+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
125+
TemporalAccessor parsedDate = formatter.parse("03-25");
126+
127+
DateTimeException exception = assertThrows(DateTimeException.class, () ->
128+
LocalDateTime.from(parsedDate)
129+
);
130+
131+
assertTrue(exception.getMessage().contains("Unable to obtain LocalDateTime from TemporalAccessor"));
132+
}
133+
134+
// Fixed Version: Correctly using MonthDay
135+
@Test
136+
void givenMonthDay_whenParsedCorrectly_thenSucceeds() {
137+
MonthDay monthDay = MonthDay.parse("03-25", DateTimeFormatter.ofPattern("MM-dd"));
138+
assertEquals(MonthDay.of(3, 25), monthDay);
139+
}
140+
141+
// Fixed Version: Combine MonthDay with LocalDate and LocalTime
142+
@Test
143+
void givenMonthDay_whenCombinedWithYearAndTime_thenSucceeds() {
144+
MonthDay monthDay = MonthDay.parse("03-25", DateTimeFormatter.ofPattern("MM-dd"));
145+
LocalDate date = LocalDate.of(2024, monthDay.getMonth(), monthDay.getDayOfMonth());
146+
LocalTime time = LocalTime.of(14, 30);
147+
LocalDateTime dateTime = LocalDateTime.of(date, time);
148+
149+
assertNotNull(dateTime);
150+
assertEquals(LocalDateTime.of(2024, 3, 25, 14, 30), dateTime);
151+
}
152+
}

0 commit comments

Comments
 (0)