简体   繁体   中英

How to calculate week number for given date Android

I am trying to get the the current week number from the given date. ie 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..

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

    Calendar calender = Calendar.getInstance();
    Log.d("Current Week:" + calender.get(Calendar.WEEK_OF_YEAR));
Calendar sDateCalendar = new GregorianCalendar(2013,03,01);
Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);

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

Calendar calender = Calendar.getInstance(); Log.d("Current Week:" + calender.get(Calendar.WEEK_OF_YEAR));

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