|
1 | 1 | package com.baeldung.date;
|
2 | 2 |
|
| 3 | +import org.joda.time.DateTime; |
| 4 | +import org.joda.time.Weeks; |
3 | 5 | import org.joda.time.Days;
|
4 | 6 | import org.joda.time.Minutes;
|
| 7 | + |
5 | 8 | import org.junit.Test;
|
6 | 9 |
|
7 | 10 | import java.text.ParseException;
|
|
17 | 20 | import java.util.Locale;
|
18 | 21 | import java.util.TimeZone;
|
19 | 22 | import java.util.concurrent.TimeUnit;
|
20 |
| - |
| 23 | + |
21 | 24 | import static org.junit.Assert.*;
|
22 | 25 |
|
23 | 26 | public class DateDiffUnitTest {
|
@@ -66,6 +69,16 @@ public void givenTwoDatesInJava8_whenUsingPeriod_thenWeGet0Year1Month29Days() {
|
66 | 69 | assertArrayEquals(new int[] { 0, 1, 29 }, new int[] { years, months, days });
|
67 | 70 | }
|
68 | 71 |
|
| 72 | + @Test |
| 73 | + public void givenTwoLocalDatesInJava8_whenUsingChronoUnitWeeksBetween_thenFindIntegerWeeks() { |
| 74 | + LocalDate startLocalDate = LocalDate.of(2024, 01, 10); |
| 75 | + LocalDate endLocalDate = LocalDate.of(2024, 11, 15); |
| 76 | + |
| 77 | + long weeksDiff = ChronoUnit.WEEKS.between(startLocalDate, endLocalDate); |
| 78 | + |
| 79 | + assertEquals(44, weeksDiff); |
| 80 | + } |
| 81 | + |
69 | 82 | @Test
|
70 | 83 | public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
|
71 | 84 | LocalDateTime now = LocalDateTime.now();
|
@@ -97,6 +110,16 @@ public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
|
97 | 110 | assertEquals(6, diff);
|
98 | 111 | }
|
99 | 112 |
|
| 113 | + @Test |
| 114 | + public void givenTwoZonedDateTimesInJava8_whenUsingChronoUnitWeeksBetween_thenFindIntegerWeeks() { |
| 115 | + ZonedDateTime startDateTime = ZonedDateTime.parse("2022-02-01T00:00:00Z[UTC]"); |
| 116 | + ZonedDateTime endDateTime = ZonedDateTime.parse("2022-10-31T23:59:59Z[UTC]"); |
| 117 | + |
| 118 | + long weeksDiff = ChronoUnit.WEEKS.between(startDateTime, endDateTime); |
| 119 | + |
| 120 | + assertEquals(38, weeksDiff); |
| 121 | + } |
| 122 | + |
100 | 123 | @Test
|
101 | 124 | public void givenTwoDateTimesInJava8_whenDifferentiatingInSecondsUsingUntil_thenWeGetTen() {
|
102 | 125 | LocalDateTime now = LocalDateTime.now();
|
@@ -127,6 +150,27 @@ public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() {
|
127 | 150 |
|
128 | 151 | }
|
129 | 152 |
|
| 153 | + @Test |
| 154 | + public void givenTwoDateTimesInJodaTime_whenComputingDistanceInWeeks_thenFindIntegerWeeks() { |
| 155 | + DateTime dateTime1 = new DateTime(2024, 1, 17, 15, 50, 30); |
| 156 | + DateTime dateTime2 = new DateTime(2024, 6, 3, 10, 20, 55); |
| 157 | + |
| 158 | + int weeksDiff = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks(); |
| 159 | + |
| 160 | + assertEquals(19, weeksDiff); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void givenTwoDateTimesInJodaTime_whenComputingDistanceInDecimalWeeks_thenFindDecimalWeeks() { |
| 165 | + DateTime dateTime1 = new DateTime(2024, 1, 17, 15, 50, 30); |
| 166 | + DateTime dateTime2 = new DateTime(2024, 6, 3, 10, 20, 55); |
| 167 | + |
| 168 | + int days = Days.daysBetween(dateTime1, dateTime2).getDays(); |
| 169 | + float weeksDiff=(float) (days/7.0); |
| 170 | + |
| 171 | + assertEquals(19.571428, weeksDiff,0.001); |
| 172 | + } |
| 173 | + |
130 | 174 | @Test
|
131 | 175 | public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
|
132 | 176 | hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
|
|
0 commit comments