简体   繁体   中英

Get Date of Specific Week

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));
} }

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM