Skip to content

Commit f324b4d

Browse files
authored
BAEL-9203 Getting the Number of Weeks Between Two Dates in Java (#18408)
* Update pom.xml * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java * Update DateDiffUnitTest.java
1 parent ecb7f6d commit f324b4d

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

core-java-modules/core-java-date-operations/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
</build>
5050

5151
<properties>
52-
<joda-time.version>2.12.5</joda-time.version>
52+
<joda-time.version>2.13.1</joda-time.version>
5353
<hirondelle-date4j.version>RELEASE</hirondelle-date4j.version>
5454
</properties>
5555

56-
</project>
56+
</project>

core-java-modules/core-java-date-operations/src/test/java/com/baeldung/date/DateDiffUnitTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.baeldung.date;
22

3+
import org.joda.time.DateTime;
4+
import org.joda.time.Weeks;
35
import org.joda.time.Days;
46
import org.joda.time.Minutes;
7+
58
import org.junit.Test;
69

710
import java.text.ParseException;
@@ -17,7 +20,7 @@
1720
import java.util.Locale;
1821
import java.util.TimeZone;
1922
import java.util.concurrent.TimeUnit;
20-
23+
2124
import static org.junit.Assert.*;
2225

2326
public class DateDiffUnitTest {
@@ -66,6 +69,16 @@ public void givenTwoDatesInJava8_whenUsingPeriod_thenWeGet0Year1Month29Days() {
6669
assertArrayEquals(new int[] { 0, 1, 29 }, new int[] { years, months, days });
6770
}
6871

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+
6982
@Test
7083
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
7184
LocalDateTime now = LocalDateTime.now();
@@ -97,6 +110,16 @@ public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
97110
assertEquals(6, diff);
98111
}
99112

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+
100123
@Test
101124
public void givenTwoDateTimesInJava8_whenDifferentiatingInSecondsUsingUntil_thenWeGetTen() {
102125
LocalDateTime now = LocalDateTime.now();
@@ -127,6 +150,27 @@ public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() {
127150

128151
}
129152

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+
130174
@Test
131175
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
132176
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());

0 commit comments

Comments
 (0)