Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine weeks between 2 LocalDate objects

I did try to search for the answer, but haven't found it.

I want to decide if and how many weeks to switch if a date is not visible on the website. I tried it with days

    LocalDate dateToSelect = parseStringDateToLocalDate(datum);
    LocalDate lastVisibleOrderDate = getLastVisibleOrderDate();
    int daysDifference = Period.between(lastVisibleOrderDate,dateToSelect).getDays();]
    int weeksToSwitch = (daysDifference / 7) + 1;

and with Period:

    Long weeks = ChronoUnit.WEEKS.between(lastVisibleOrderDate,dateToSelect);

However, I can't figure out how to account for the 7 day difference and still be in the same week.

Scenario: Last visible date = 2017-03-12 (Sunday) Date to select = 2017-03-13 (Monday)

Difference = 1. divided by 7 = 0, plus 1, so switch to next week. That is great! Works.

However, if the date to select is 2017-03-19 (Sunday) the difference = 7. Divided by 7 and plus 1 = 2. It would switch 2 weeks but it only needs to switch 1.

It should be able to switch weeks back and forth, so adding up a day to lastVisibleOrderDate or minus 1 day on dateToSelect would give problems with that.

I could do this (for the positive switching), but it isn't the nicest option I hope:

    if (0 < daysDifference && daysDifference <= 7){

    } else if (7 < daysDifference && daysDifference <=14){

    } else if (14 < daysDifference && daysDifference <=21){

    } else if (21 < daysDifference && daysDifference <=28){

    }

Any suggestions?

like image 555
Blitzoff Avatar asked Oct 27 '25 17:10

Blitzoff


1 Answers

You want to use ChronoUnit to measure time.

In your questions the if/else you have is really measuring a 8 day week. If you subtract two days the answer is 0 based, the lowest number is 0, not 1. So 7 days is really a week + 1 day in your example.

I believe this would get you the results you want.

LocalDate lastVisible = LocalDate.of(2017, 3, 12);
LocalDate dateToSelect = LocalDate.of(2017, 3, 13).minusDays(1);

long weeks = ChronoUnit.WEEKS.between(lastVisible, dateToSelect);
System.out.println("number of weeks "+ weeks);

dateToSelect = LocalDate.of(2017, 3, 19).minusDays(1);
weeks = ChronoUnit.WEEKS.between(lastVisible, dateToSelect);
System.out.println("number of weeks "+ weeks);

dateToSelect = LocalDate.of(2017, 3, 20).minusDays(1);
weeks = ChronoUnit.WEEKS.between(lastVisible, dateToSelect);
System.out.println("number of weeks "+ weeks);

-- output :

number of weeks 0
number of weeks 0
number of weeks 1

BUT this is to get the actual number of weeks.

LocalDate lastVisible = LocalDate.of(2017, 3, 12);
LocalDate dateToSelect = LocalDate.of(2017, 3, 13);

long weeks = ChronoUnit.WEEKS.between(lastVisible, dateToSelect);
System.out.println("number of weeks "+ weeks);

dateToSelect = LocalDate.of(2017, 3, 19);
weeks = ChronoUnit.WEEKS.between(lastVisible, dateToSelect);
System.out.println("number of weeks "+ weeks);

// to get number of weeks regardless of going forward or backwards in time
dateToSelect = LocalDate.of(2017, 3, 20);
weeks = Math.abs(ChronoUnit.WEEKS.between(dateToSelect, lastVisible));
System.out.println("number of weeks "+ weeks);

-- output:

number of weeks 0
number of weeks 1
number of weeks 1
like image 182
denov Avatar answered Oct 30 '25 06:10

denov