-3

I need to extract the start date and end date from a given year and week and return them as LocalDate:

Example: year / month / week : 2022 / 12 / 49 -> date_begin 05/12/2022 - date_end 11/12/2022 this mean the week 49 of the year 2022 starts from 05/12/2022 and ends on the 11/12/2022. The month is irrelevant, as @rzwitserloot said in the comments. The input is provided in ints int year = 2022 and int week = 49.

How to achieve this?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Jsef bch
  • 71
  • 1
  • 7
  • 5
    Yes, the `java.time` package. – Michael Dec 08 '22 at 12:00
  • 2
    `LocalDate` offers all you need – XtremeBaumer Dec 08 '22 at 12:00
  • 3
    Yes and no. The concept of a 'year-month-week' doesn't exist in java nor in any other system; it just aint a thing. There _is_ such a thing as a year+week which java can do, i.e. you can turn '2022-49' into '2022-12-05 - 2022-12-11' no problem. Do you need the answer for how to convert year+week, completely ignoring the month value, or is there some significance to it? What if someone asks 'year = 2022, month = 5, week = 49', now what? How is this input provided? In a string, or just as 3 ints? – rzwitserloot Dec 08 '22 at 12:00
  • 1
    @rzwitserloot the month is irrelevant as you said yes! the input is provided in ints actually... int year = 2022, int week = 49 ... – Jsef bch Dec 08 '22 at 12:35

2 Answers2

2

JSR310-extra had the YearWeek, but the somewhat simpler java.time does not - hence, the simplest way is through the parser even if you don't actually need to parse it:

int weekYear = 2022;
int weekNum = 49;
LocalDate monday = LocalDate.parse(String.format("%04d-W%02d-1", weekYear, weekNum), DateTimeFormatter.ISO_WEEK_DATE);
LocalDate sunday = monday.plusDays(6);
System.out.printf("Week %d of year %d runs from %s to %s\n", weekNum, weekYear, monday, sunday);

NB: The format is e.g. 2022-W49-1; the 1 is for 'monday'. Note that this is weekyears: That means the start date could be in the previous year (e.g. week 1 of certain years starts on december 30th in the previous year), or the end date could be in the next year. This is obvious if you think about it (weeks exist that start in one year and end in the next, and they have to be part of some year's 'week-year' system). Just thought I'd highlight it :)

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 1
    Rather than formatting and then parsing I would prefer `LocalDate.of(weekYear, Month.FEBRUARY, 1).with(WeekFields.ISO.weekOfWeekBasedYear(), weekNum)` and then adjusting to first and last day of the week, respectively. Neither solution is perfect though, and I find both acceptable. – Ole V.V. Dec 10 '22 at 12:58
  • I do not see any `YearWeek` class in the original [JSR 310](https://jcp.org/en/jsr/detail?id=310) API. The first clause of your first sentence seems incorrect. – Basil Bourque Dec 11 '22 at 20:21
  • It's from 310-extra. I linked it. – rzwitserloot Dec 11 '22 at 21:10
-1

This solution also works

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;

public class Main {
    
    public static final WeekFields US_WEEK_FIELDS = WeekFields.of(DayOfWeek.SUNDAY, 4);

    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2022, 12, 29);
        LocalDate date2 = LocalDate.now();
        System.out.println(formatDate(date1));
        System.out.println(formatDate(date2));
    }
    
    public static int getWeek(LocalDate date) {
        return date.get(US_WEEK_FIELDS.weekOfWeekBasedYear());
    }
    
    public static int getMonth(LocalDate date) {
        return date.getMonthValue();
    }

    public static int getYear(LocalDate date) {
        return date.get(US_WEEK_FIELDS.weekBasedYear());
    }
    
    public static String formatDate(LocalDate date) {
        int week = getWeek(date);
        int month = getMonth(date);
        int year = getYear(date);
        return year + "/" + month + "/" + week;
    }

}

When running I get in the console

2022/12/52
2022/12/49
  • 3
    The question is: "Given a year and a weeknumber, get me the 7 dates that fall in that year in LocalDate form". This answer answers a different question, namely: "Given a date in LocalDate form, get me the year, month, and weeknumber" - that's the reverse of what is being asked. – rzwitserloot Dec 08 '22 at 14:37
  • 1
    This seems to give the opposite conversion of the one asked for. – Ole V.V. Dec 08 '22 at 20:48