Search code examples
javajodatime

Get Date in Java given Week number, week day and year


Given Week of the year, the week day and the year, how can we get the Date in Java?

With Jodatime, I tried the following:

DateTime dt = new DateTime();
dt.withYear(year);
dt.withWeekOfWeekyear(weekOfYear);
dt.withDayOfWeek(weekDay);
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd");
System.out.println(dateTimeFormatter.print(dt));

But it gets the current Date!


Solution

  • JodaTime returns a changed copy, so do:

    DateTime dt = new DateTime()
        .withWeekyear(year)
        .withWeekOfWeekyear(weekOfYear)
        .withDayOfWeek(weekDay);
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd");
    System.out.println(dateTimeFormatter.print(dt));
    

    And this should work as expected.