1

I trying to get the week numbers which fall between a given start date and end date in Java. This is ISO8601 date.

Example 
startDate - "2018-08-24T12:18:06,166"
endDate -  "2019-08-24T11:18:06,166"

The current week number is 34.

For this example, I would get 34,35,36... Last week number of 2018..1,2...last week number of 2019 and so on

Is there a good solution to this ?

Presently I got it worked with same year date range, what I tried is, I get the start week number and end week number from the start date and end date, and then I loop it giving the start value and end value. But if the date range falls in multiple years, how would it be? Can any one help me

Java Programmer
  • 1,347
  • 3
  • 12
  • 31

2 Answers2

4

If you are using Java 8 you can use java.time API Like so :

int addWeek = 0;
if(startDate.get(WeekFields.ISO.weekOfYear()) < endDate.get(WeekFields.ISO.weekOfYear())){
    addWeek = 1;
}
long weeks = WEEKS.between(startDate, endDate) + addWeek;//Get the number of weeks in your case (52)
List<Integer> numberWeeks = new ArrayList<>();
if (weeks >= 0) {
    int week = 0;
    do {
        //Get the number of week
        int weekNumber = startDate.plusWeeks(week).get(WeekFields.ISO.weekOfYear());
        numberWeeks.add(weekNumber);
        week++;
    } while (week <= weeks);
}

Ideone demo

[34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 
 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]

Note that you get both week numbers from both years, weeks of 2018 [34-52], then weeks of 2019 [1-33]

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • @JavaProgrammer check my edit, in this case I would like to use `do{}while` loop instead, don't forgot to check if the number of weeks is great or equals 0 – Youcef LAIDANI Aug 24 '18 at 15:52
  • But when i checked with below two dates 2018-08-24T12:18:06,166 and 2018-08-30T12:18:06,166 Ideally i should get 34 and 35 right ? since the first date falls into week number 34 and the second date falls into week number 35 – Java Programmer Aug 24 '18 at 16:10
  • 1
    Perfect. Thank you so much for this help. You are so quick :) – Java Programmer Aug 24 '18 at 16:44
  • YCF_L , is there an option to set the first day of the week as Sunday here ? I think its by default taking first day as Monday? @YCF_L – Java Programmer Aug 26 '18 at 12:24
  • @JavaProgrammer yes you can use `startDate = startDate.with(DayOfWeek.SUNDAY);`, the same for the `endDate = endDate.with(DayOfWeek.SUNDAY);` if you want – Youcef LAIDANI Aug 26 '18 at 12:29
  • So if i set dayOfWeek to sunday and considering these two dates 2018-08-26T12:18:06,166 and 2018-08-26T12:19:06,188 . I should be getting 35 as the week number instead of 34 right ? I did that but i am still getting the week number as 34 for the date of 26th. And for the date 27th i am getting 35 @YCF_L – Java Programmer Aug 26 '18 at 13:03
  • 1
    @JavaProgrammer ah I don't understand your question in this case you can use `WeekFields.SUNDAY_START.weekOfYear()` instead of `WeekFields.ISO.weekOfYear()` your code can look like this :https://ideone.com/d8HKnN – Youcef LAIDANI Aug 26 '18 at 13:17
  • 1
    Okay , i think this will do .Thank you – Java Programmer Aug 26 '18 at 14:14
  • if i put these two dates 2018-08-22T08:54:43,907 and 2018-08-29T08:54:43,907 I should get ideally 34 and 35 right . But getting 34,35, and 36 too. Even before week 36 is started, @YFCL – Java Programmer Aug 29 '18 at 17:03
  • @JavaProgrammer for a better solution I would like to create a new question so we can help you, the question and answer should be for only one problem, so please create a new question where you can share this solution and the problem you get and I'm sure you will get good response :) – Youcef LAIDANI Aug 29 '18 at 17:05
  • https://stackoverflow.com/questions/52083103/week-numbers-between-two-dates-in-java A new question asked – Java Programmer Aug 29 '18 at 17:24
  • @JavaProgrammer I tried with that inputs an I get the desired outputs https://ideone.com/144kcr – Youcef LAIDANI Aug 29 '18 at 19:00
  • I believe your condition for setting `addWeek` to 1 is incorrect. From 2017-12-26 to 2018-01-05, I expect [52, 1], which I got. From 2017-12-29 to 2018-01-02, I expect the same, but got only [52]. From 2018-01-02 to 2018-01-12 I expect [1, 2], I got [1, 2, 3]. – Ole V.V. Aug 29 '18 at 20:23
  • I will review it tomorrow, I'm really so tired now @OleV.V. – Youcef LAIDANI Aug 29 '18 at 20:51
3

The Answer by YCF_L is correct.

YearWeek

You may want to add the ThreeTen-Extra library to your project to make use of its YearWeek class. This class represents a specific week using the standard ISO 8601 definition of a week: starts on a Monday, week # 1 contains the first Thursday of the calendar-year, the last few ending/beginning days of the calendar-year may land in the next/previous week-based-year.

YearWeek ywStart = YearWeek.from( startDate ) ;

Increment as shown in the other Answer, but collecting YearWeek objects rather than integer numbers.

ArrayList< YearWeek > yearWeeks = new ArrayList<>( countOfWeeks + 2 ) ;  // Add one or two for good measure. 
…
YearWeek yw = yw.plusWeeks( 1 ) ;
yearWeeks.add( yw ) ;

To report, call the YearWeek::toString method to generate text in standard ISO 8601 format: yyyy-Www such as 2018-W43.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    `2018-W43` is more clear than `43` nice one Basil, so I should to make my code like this to get your result [ideone demo](https://ideone.com/i2MZtB) – Youcef LAIDANI Aug 24 '18 at 16:39
  • Thanks Basil and YCF_L – Java Programmer Aug 24 '18 at 16:47
  • 3
    @YCF_L When generating your own such ISO 8601 string, be sure to pull the *week-based year number* rather than the *calendar year*. If doing much work at all with weeks or quarters, I recommend downloading the *ThreeTen-Extra* library rather than re-inventing it. – Basil Bourque Aug 24 '18 at 17:02