0

I'm trying to get the start and end date of specific week of a month. However the date is incorrect. Can anyone identify what's the issue ?

public class DateUtils
{
   getWeeklyDateList(2020,5, 3);

  public static void getWeeklyDateList(int year, int month, int week)
  {
      Calendar calendar = Calendar.getInstance(Locale.getDefault());
      // setting year, month, week
      calendar.set(Calendar.YEAR, year);
      calendar.set(Calendar.MONTH, month);
      calendar.set(Calendar.WEEK_OF_MONTH,week);

    // setting day of week to first --> Sunday
    calendar.set(Calendar.DAY_OF_WEEK, 1);

    int year1 = calendar.get(Calendar.YEAR);
    int month1 = calendar.get(Calendar.MONTH);
    int day1 = calendar.get(Calendar.DAY_OF_MONTH);

    // setting day of week to last --> Saturday
    calendar.set(Calendar.DAY_OF_WEEK, 7);

    int year7 = calendar.get(Calendar.YEAR);
    int month7 = calendar.get(Calendar.MONTH);
    int day7 = calendar.get(Calendar.DAY_OF_MONTH);

    Log.e("date_start", String.valueOf(year1) + "-" + String.valueOf(month1) + "-" + String.valueOf(day1));
    Log.e("date_end", String.valueOf(year7) + "-" + String.valueOf(month7) + "-" + String.valueOf(day7));
} }
Aan
  • 289
  • 5
  • 15
  • Are you expecting start to be 2020-6-14 and end 2020-6-20? What are you getting instead? Please remember, *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. See: How to create a Minimal, Reproducible Example.* From [*What topics can I ask about here?*](https://stackoverflow.com/help/on-topic) – Ole V.V. May 25 '20 at 15:37

2 Answers2

3

java.time and ThreeTenABP

If you want to use java.time, the modern Java date and time API, it can be done with this simple method:

private static WeekFields wf = WeekFields.of(Locale.forLanguageTag("ne-NP"));

public static void getWeeklyDateList(int year, Month month, int week) {
    LocalDate someDateInTheWeek = LocalDate.of(year, month, 10).with(wf.weekOfMonth(), week);
    LocalDate start = someDateInTheWeek.with(wf.dayOfWeek(), 1);
    LocalDate end = someDateInTheWeek.with(wf.dayOfWeek(), 7);

    System.out.println("date_start: " + start);
    System.out.println("date_end:   " + end);
}

Trying it out with your example arguments:

    getWeeklyDateList(2020, Month.JUNE, 3);

Output is:

date_start: 2020-06-14
date_end:   2020-06-20

How it works:

First, weeks are defined differently in different cultures. In Nepal (since you give Kathmandu, Nepal as your location) weeks start on Sunday and are numbered in a way where the 1st of the month is in week 1 of the month. To handle this week scheme I am initializing a WeekFields object for Nepalese culture.

LocalDate is the java.time class for a date without time of day. I don’t think it matters which day of the month I pick as a starting point; I took the 10th. From that date I get a date in the correct week, using the WeekFields object and the supplied week number. From there in turn I get the first and the last day of the week, again according to the Nepalese definition of weeks: from Sunday June 14 through Saturday June 20 2020.

What went wrong in your code I cannot tell. In any case the Calendar class you used is poorly designed and long outdated. It also default uses the default locale of the JVM for its week definition, which may have given you a different week scheme from what you wanted. A final point that may have confused you: Calendar unnaturally numbers months from 0 for January through 11 for December. So when you specified 5, you got June (not May). You printed out the month numbers of your result dates, which probably again printed 5 (not 6) for June.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Most of your as well as Basil Bourque's answers on the topic of date/time are like a compact chapter on date & time. It not only solves the problem but also helps one learn about the nitty-gritty of old and new date & time API. I must say that the credit of most my knowledge about the modern date & time API goes to both of you. – Arvind Kumar Avinash May 25 '20 at 16:39
  • 2
    Thanks, @ArvindKumarAvinash, for the nice words. They matter. – Ole V.V. May 25 '20 at 16:40
0

Its because of this line, calendar.set(Calendar.DAY_OF_WEEK, 1);

this can take month to previous month if week starts in previous one. If there is a month difference then this is the problem.

  • So how can we get 1st and last date of that week. In the above code? – Aan May 25 '20 at 13:25
  • I think Ole V.V. solution is perfect in this case.But if you still have to use this solution you can loop through after setting year and month. like while (calendar.get(Calendar.MONTH) == month) then check for sundays according to weeks. – Faizan Burney May 26 '20 at 11:02