0
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.set(Calendar.WEEK_OF_YEAR, 0);
    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
    cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println(formatter1.format(cal.getTime()));
    cal.add(Calendar.DAY_OF_WEEK, 6);
    System.out.println(formatter1.format(cal.getTime()));

I want to get the start and end date of a given week number in Java.

One example output:

2020-12-20
2020-12-26

Another example:

2021-01-03
2021-01-09
Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
  • 3
    Please add the results from IDE executions for better understanding of problem. Also make sure that both Idea and Android studio has same system datetime set up. – Taras Shpulyar Jul 08 '21 at 19:58
  • 1
    Your are using terrible date-time classes that were years ago supplanted by the modern *java.time* classes defined in JSR 310. Apparently you want the first Sunday of the current year. `Year.now( ZoneId.systemDefault() ).atDay(1).with( TemporalAdjusters.nextOrSame( DayOfWeek.SUNDAY ) ).format( DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( Locale.getDefault() ) )` – Basil Bourque Jul 08 '21 at 20:56
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends like `Calendar`. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 08 '21 at 21:58
  • Different locales have different ideas about week numbers and also different ideas about on which day of week a new week begins. You are no doubt seeing a difference in default locale setting. – Ole V.V. Jul 08 '21 at 22:07

0 Answers0