|
1 | 1 | package com.baeldung.datetime; |
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | + |
3 | 6 | import java.time.DayOfWeek; |
4 | 7 | import java.time.LocalDate; |
5 | 8 | import java.time.LocalDateTime; |
| 9 | +import java.time.LocalTime; |
6 | 10 |
|
7 | 11 | import org.junit.Test; |
8 | 12 |
|
9 | | -import static org.junit.Assert.assertEquals; |
10 | | - |
11 | 13 | public class UseLocalDateUnitTest { |
12 | 14 |
|
13 | 15 | UseLocalDate useLocalDate = new UseLocalDate(); |
@@ -57,4 +59,33 @@ public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight() { |
57 | 59 | assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22"))); |
58 | 60 | } |
59 | 61 |
|
| 62 | + @Test |
| 63 | + public void givenLocalDate_whenSettingStartOfDay_thenReturnMidnightInAllCases() { |
| 64 | + LocalDate given = LocalDate.parse("2018-06-23"); |
| 65 | + |
| 66 | + LocalDateTime startOfDayWithMethod = useLocalDate.getStartOfDay(given); |
| 67 | + LocalDateTime startOfDayOfLocalDate = useLocalDate.getStartOfDayOfLocalDate(given); |
| 68 | + LocalDateTime startOfDayWithMin = useLocalDate.getStartOfDayAtMinTime(given); |
| 69 | + LocalDateTime startOfDayWithMidnight = useLocalDate.getStartOfDayAtMidnightTime(given); |
| 70 | + |
| 71 | + assertThat(startOfDayWithMethod).isEqualTo(startOfDayWithMin) |
| 72 | + .isEqualTo(startOfDayWithMidnight) |
| 73 | + .isEqualTo(startOfDayOfLocalDate) |
| 74 | + .isEqualTo(LocalDateTime.parse("2018-06-23T00:00:00")); |
| 75 | + assertThat(startOfDayWithMin.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT); |
| 76 | + assertThat(startOfDayWithMin.toString()).isEqualTo("2018-06-23T00:00"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void givenLocalDate_whenSettingEndOfDay_thenReturnLastMomentOfDay() { |
| 81 | + LocalDate given = LocalDate.parse("2018-06-23"); |
| 82 | + |
| 83 | + LocalDateTime endOfDayWithMax = useLocalDate.getEndOfDay(given); |
| 84 | + LocalDateTime endOfDayFromLocalTime = useLocalDate.getEndOfDayFromLocalTime(given); |
| 85 | + |
| 86 | + assertThat(endOfDayWithMax).isEqualTo(endOfDayFromLocalTime); |
| 87 | + assertThat(endOfDayWithMax.toLocalTime()).isEqualTo(LocalTime.MAX); |
| 88 | + assertThat(endOfDayWithMax.toString()).isEqualTo("2018-06-23T23:59:59.999999999"); |
| 89 | + } |
| 90 | + |
60 | 91 | } |
0 commit comments