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()));
I am still working on it. Can someone help me fix this?
java date calendar
user1514499
source share