I am not sure if I am posting this under the right thread or not but here it goes. I ran into a little problem while using the Calendar class. At work our work weeks are from Monday to Sunday and I developed a report to show data for each day in the work week. So I created the method below to get the Sunday date of the week that the selected date the user selects falls in. So a user selects a Wednesday date, that date (Calendar instance) is passed into the method and the following Sunday (Calendar instance) is returned, works fine. The issue was coming that I was getting the month, for a monthly total, after this method was called and the date of the passed date was also changed to that Sunday's date and I am not sure why and/or how. Simply work around for me was to get the month before this method was called but I want to try and figure out what I am doing to cause this so I can prevent myself from doing this in the future. The code below produces the following output. Any insight or helpful links would be appreciated. Thanks
The month before getting sundays date 8
The month after getting sundays date 9public class DateClass { public DateClass() { } public Calendar getSundaysDate(Calendar passedDate) { Calendar newDate = passedDate; int day = newDate.get(Calendar.DAY_OF_WEEK); if (day != 1) { int dayIterator = day; while (dayIterator < 8) { newDate.add(Calendar.DAY_OF_MONTH, 1); dayIterator++; } } return newDate; } public static void main(String[] args) { SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); Date date = new Date(); try { date = df.parse("2012/08/28"); } catch (Exception ex) { ex.printStackTrace(); } Calendar cal = Calendar.getInstance(); cal.setTime(date); DateClass dateClass = new DateClass(); System.out.println("The month before getting sundays date " + (cal.get(Calendar.MONTH) + 1)); Calendar newCal = dateClass.getSundaysDate(cal); System.out.println("The month after getting sundays date " + (cal.get(Calendar.MONTH) + 1)); } }