Java - to get the start date and end date of each week for a given month

Below is the code that I use to calculate the start date of the week and the end date for this month. Suppose the week start date is MONDAY and the week end day is SUNDAY. For example, in JANUARY 2013 there will be 5 weeks. If the month starts on Sunday, ignore this day.

January 2013 the first week is from December 31 to January 6, 2013 the second week is from January 7 to 2013 to January 13, 2013 The third week is January 14, 2013 to January 20, 2013 the fourth week is January 21, 2013 to 27 January 2013 fifth week - January 28, 2013 to February 03, 2013

public static void main(String[] args) { List<List<String>> weekdates = getNumberOfWeeks(2013, Calendar.JULY); for(List<String> weekDatesLoop:weekdates){ System.out.println("Start day: "+weekDatesLoop.get(0).toString()); System.out.println("End day: "+weekDatesLoop.get(1).toString()); } } public static List<List<String>> getNumberOfWeeks(int year, int month) { System.out.println("Month Id: "+month); month = month-1; System.out.println("Month Id: " + month); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); List<List<String>> weekdates = new ArrayList<List<String>>(); List<String> dates = new ArrayList<String>(); Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, 1); dates.add(format.format(c.getTime())); //int numOfWeeksInMonth = 1; while (c.get(Calendar.MONTH) == month) { //System.out.println(c.get(Calendar.DAY_OF_WEEK) ); if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { dates.add(format.format(c.getTime())); weekdates.add(dates); } else if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { dates = new ArrayList<String>(); dates.add(format.format(c.getTime())); //numOfWeeksInMonth++; } c.add(Calendar.DAY_OF_MONTH, 1); } if(dates.size() < 2){ c.add(Calendar.DAY_OF_MONTH, -1); dates.add(format.format(c.getTime())); weekdates.add(dates); } System.out.println(weekdates); return weekdates; } 

I am still working on it. Can someone help me fix this?

+2
java date calendar
source share
2 answers

I get an answer with the following code

 List<List<String>> getNumberOfWeeks(int year, int month) { SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy"); List<List<String>> weekdates = new ArrayList<List<String>>(); List<String> dates; Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); c.set(Calendar.DAY_OF_MONTH, 1); while (c.get(Calendar.MONTH) == month) { dates = new ArrayList<String>(); while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) { c.add(Calendar.DAY_OF_MONTH, -1); } dates.add(format.format(c.getTime())); c.add(Calendar.DAY_OF_MONTH, 6); dates.add(format.format(c.getTime())); weekdates.add(dates); c.add(Calendar.DAY_OF_MONTH, 1); } System.out.println(weekdates); return weekdates; } 
+1
source share

A possible solution would be to not reinvent the wheel and use JodaTime or a similar library. For example, you can use the dayOfWeek () function on DateTime to get the information that you are after.

+1
source share

All Articles