简体   繁体   中英

Getting dates from week number of year in android

I am trying to get the dates from a given weeknumber and the year. The below piece of code is always showing the currentDates even though I enter week and year.

Calendar c = new GregorianCalendar(Locale.getDefault());
    c.set(Calendar.WEEK_OF_YEAR, 15);
    c.set(Calendar.YEAR, 2015);

    String result = "";

    int firstDayOfWeek = c.getFirstDayOfWeek();
    for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
        c.set(Calendar.DAY_OF_WEEK, i);
        result += new SimpleDateFormat("yyyy.MM.dd").format(c.getTime()) + "\n";
    }

instead of getting dates of Week number 15. I am getting current dates values always. I am not what is wrong here?

How to solve this issue?

Thanks!

Try this

  Calendar now = Calendar.getInstance();
  now.set(Calendar.YEAR,2015);
  now.set(Calendar.WEEK_OF_YEAR,15);

  System.out.println(new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()));

OUTPUT

在此处输入图片说明

please update the my code: it's works for me.

  Calendar now = Calendar.getInstance();
      now.set(Calendar.YEAR,2015);
      now.set(Calendar.WEEK_OF_YEAR,15);


      String result = "";

        int firstDayOfWeek = now.getFirstDayOfWeek();

        Log.e("day","day:"+firstDayOfWeek);
        for (int i = firstDayOfWeek; i < firstDayOfWeek + 7; i++) {
            now.set(Calendar.DAY_OF_WEEK, i);
            result += new SimpleDateFormat("yyyy.MM.dd").format(now.getTime()) + "\n";
        }

    Log.e("result","result:"+result);

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