-2

how to get first and last date of weeks on basis of month and year in android ex:we pass month and year (March,2016) then i want all weeks just like mar5- mar11,(sat to fri) mar12- mar18, mar19- mar25, mar26-april01

please help me

3 Answers3

2

Call this function to get results in a valid week pair list of given month of a year.

ex: getWeekStartEnd("December","2016");

Result: [ December3-December9, December10-December16, December17-December23, December24-December30 ]

List<String> getWeekStartEnd (String month , String year) {
        List<String> validWeekPairs = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMMM-yyyy");
        String day = "01";
        try {
            Date date = sdf.parse(day + "-" + month + "-" + year);
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTime(date);
            calendar.setFirstDayOfWeek(Calendar.SATURDAY);

            List<String> startDayOfWeek = new ArrayList<>();
            List<String> endDayOfWeek = new ArrayList<>();
            int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            boolean isStartDaySet = false;
            boolean hasLastDayOfWeek = true;
            for (int currentDay = 01; currentDay <= daysInMonth; currentDay++) {
                Date newDate = sdf.parse(currentDay + "-" + month + "-" + year);
                calendar.setTime(newDate);
                int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                if (dayOfWeek == Calendar.SATURDAY) {
                    if (hasLastDayOfWeek) {
                        startDayOfWeek.add(month + String.valueOf(currentDay));
                        isStartDaySet = true;
                        hasLastDayOfWeek = false;
                    }
                } else if (dayOfWeek == Calendar.FRIDAY) {
                    if (isStartDaySet) {
                        endDayOfWeek.add(month + String.valueOf(currentDay));
                        hasLastDayOfWeek = true;
                    }
                }


            }


            for (int i = 0; i < endDayOfWeek.size(); i++) {
                validWeekPairs.add(startDayOfWeek.get(i) + "-" + endDayOfWeek.get(i));
            }

            return validWeekPairs;

        } catch (ParseException e) {
            e.printStackTrace();
            return validWeekPairs;

        }catch (Exception e){
            e.printStackTrace();
            return validWeekPairs;
        }

    }
Sadiq Md Asif
  • 882
  • 6
  • 18
  • ,Yes this is actual solution Bro...Its work for me .Thanks alot. – Shubham Verma Dec 21 '16 at 11:15
  • Yes sure but my reputation is less. – Shubham Verma Dec 21 '16 at 12:09
  • There is one problem .This method doesn't fetch last week if it is partially in other month for ex:getWeekStartEnd( January,2017) result: January7-January13 January14-January20 January21-January27 but we dont get this below week: January28- February3 Then how we solve this problem – Shubham Verma Dec 21 '16 at 12:59
  • From your question , i designed the function to get the full weeks of a particular month! Anyways , i will try to give a update later! – Sadiq Md Asif Dec 21 '16 at 16:45
1

If you are using java.util.Date you can use the method getDay(). It returns Calendar constant, like Calendar.MONDAY.

Ihor Dobrovolskyi
  • 1,241
  • 9
  • 19
1

You can pass the string as 02,2016

List<String> getWeekendsOftheMonth(String monthYearString) {
         long monthDate;
         List<String> weekEnds = new ArrayList<>();
         SimpleDateFormat simpleDateFormat= new SimpleDateFormat("dd,MM,yyyy");
         try {
               monthYearString="01,".concat(monthYearString);
               monthDate = simpleDateFormat.parse(monthYearString).getTime();
               Calendar calendar = Calendar.getInstance();
               calendar.setTimeInMillis(monthDate);
               int maxDate = calendar.getActualMaximum(Calendar.DATE);
               int minDate = calendar.getActualMinimum(Calendar.DATE);
               for (int i = minDate; i <= maxDate; i++) {
                   calendar.set(Calendar.DATE, i);
                   if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY|| calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                     weekEnds.add(convertToDate(calendar.getTimeInMillis()));
                   }
               }
             }catch (Exception e){
                    e.printStackTrace();
            }
    return weekEnds;
 }

    String convertToDate(Long datetime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMM. yyyy");
        Date date = new Date(datetime);
        return simpleDateFormat.format(date);
    }

you can do it from this method.

Noorul
  • 3,386
  • 3
  • 32
  • 54