7

I am trying to get the the current week number from the given date. i.e. If I enter the date as 01/03/2013 then i should get the week number which is 9..

Please help me in finding the solution..

Thanks..

Abhishek..

Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38

4 Answers4

16

You can create a Calendar object for that date and get the week with:

calendar.get(Calendar.WEEK_OF_YEAR)

The API is described here: http://developer.android.com/reference/java/util/Calendar.html#WEEK_OF_YEAR

blackgreen
  • 34,072
  • 23
  • 111
  • 129
azertiti
  • 3,150
  • 17
  • 19
  • this code gives me the total number of week in that particular year. i.e. in this year there are 52 weeks. I don't want that. I want the number of weeks from the beginning of the year i.e. the week number till today is 9.. I want this thing not the total week.. – Abhishek Dhiman Mar 01 '13 at 09:58
  • I think you do something wrong. The following code returns 9 in my case. Calendar calendar = Calendar.getInstance(); int week = calendar.get(Calendar.WEEK_OF_YEAR); – azertiti Mar 01 '13 at 10:05
11
    Calendar calender = Calendar.getInstance();
    Log.d("Current Week:" + calender.get(Calendar.WEEK_OF_YEAR));
Hulk
  • 2,565
  • 16
  • 24
4
Calendar sDateCalendar = new GregorianCalendar(2013,03,01);
Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
2

01/03/2013 is taken in as 113 , 2 ,1

            Date d = new Date(113, 2, 1);

            Calendar c = Calendar.getInstance();

            c.setTime(d);

            int weekOfYear = c.get(Calendar.WEEK_OF_YEAR);
DjP
  • 4,537
  • 2
  • 25
  • 34