这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core-java-modules/core-java-date-operations/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
</build>

<properties>
<joda-time.version>2.12.5</joda-time.version>
<joda-time.version>2.13.1</joda-time.version>
<hirondelle-date4j.version>RELEASE</hirondelle-date4j.version>
</properties>

</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.baeldung.date;

import org.joda.time.DateTime;
import org.joda.time.Weeks;
import org.joda.time.Days;
import org.joda.time.Minutes;

import org.junit.Test;

import java.text.ParseException;
Expand All @@ -17,7 +20,7 @@
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;

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

@Test
public void givenTwoLocalDatesInJava8_whenUsingChronoUnitWeeksBetween_thenFindIntegerWeeks() {
LocalDate startLocalDate = LocalDate.of(2024, 01, 10);
LocalDate endLocalDate = LocalDate.of(2024, 11, 15);

long weeksDiff = ChronoUnit.WEEKS.between(startLocalDate, endLocalDate);

assertEquals(44, weeksDiff);
}

@Test
public void givenTwoDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
LocalDateTime now = LocalDateTime.now();
Expand Down Expand Up @@ -97,6 +110,16 @@ public void givenTwoZonedDateTimesInJava8_whenDifferentiating_thenWeGetSix() {
assertEquals(6, diff);
}

@Test
public void givenTwoZonedDateTimesInJava8_whenUsingChronoUnitWeeksBetween_thenFindIntegerWeeks() {
ZonedDateTime startDateTime = ZonedDateTime.parse("2022-02-01T00:00:00Z[UTC]");
ZonedDateTime endDateTime = ZonedDateTime.parse("2022-10-31T23:59:59Z[UTC]");

long weeksDiff = ChronoUnit.WEEKS.between(startDateTime, endDateTime);

assertEquals(38, weeksDiff);
}

@Test
public void givenTwoDateTimesInJava8_whenDifferentiatingInSecondsUsingUntil_thenWeGetTen() {
LocalDateTime now = LocalDateTime.now();
Expand Down Expand Up @@ -127,6 +150,27 @@ public void givenTwoDateTimesInJodaTime_whenDifferentiating_thenWeGetSix() {

}

@Test
public void givenTwoDateTimesInJodaTime_whenComputingDistanceInWeeks_thenFindIntegerWeeks() {
DateTime dateTime1 = new DateTime(2024, 1, 17, 15, 50, 30);
DateTime dateTime2 = new DateTime(2024, 6, 3, 10, 20, 55);

int weeksDiff = Weeks.weeksBetween(dateTime1, dateTime2).getWeeks();

assertEquals(19, weeksDiff);
}

@Test
public void givenTwoDateTimesInJodaTime_whenComputingDistanceInDecimalWeeks_thenFindDecimalWeeks() {
DateTime dateTime1 = new DateTime(2024, 1, 17, 15, 50, 30);
DateTime dateTime2 = new DateTime(2024, 6, 3, 10, 20, 55);

int days = Days.daysBetween(dateTime1, dateTime2).getDays();
float weeksDiff=(float) (days/7.0);

assertEquals(19.571428, weeksDiff,0.001);
}

@Test
public void givenTwoDatesInDate4j_whenDifferentiating_thenWeGetSix() {
hirondelle.date4j.DateTime now = hirondelle.date4j.DateTime.now(TimeZone.getDefault());
Expand Down