Reputation: 771
Following is the code that I am using to calculate the week start date and end date for the given month. Assume week start day is MONDAY and week end day is SUNDAY. For example, JANUARY, 2013 will have 5 weeks. If the month starts with Sunday - ignore that day
January 2013 first week - 31-Dec-2012 to 06-Jan-2013 second week - 07-jan-2013 to 13-jan-2013 Third week - 14-jan-2013 to 20-jan-2013 fourth week - 21-jan-2013 to 27-jan-2013 fifth week - 28-jan-2013 to 03-Feb-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 this. Can anyone please help me in fixing this?
Upvotes: 3
Views: 9472
Reputation: 340098
You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310. This work is much easier now.
YearMonth
The YearMonth
class represents a specific month as a whole. Specify the month using the Month
enum.
YearMonth ym = YearMonth.of( 2013 , Month.JANUARY ) ;
LocalDate
Get the first of the month.
LocalDate firstOfMonth = ym.atDay( 1 ) ;
TemporalAdjuster
Get the previous Monday, or stay with this date if it is a Monday.
TemporalAdjuster ta = TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) ;
LocalDate previousOrSameMonday = firstOfMonth.with( ta ) ;
Then loop a week at a time.
LocalDate endOfMonth = ym.atEndOfMonth() ;
LocalDate weekStart = previousOrSameMonday ;
do {
LocalDate weekStop = weekStart.plusDays( 6 ) ;
System.out.println( "Week: " + weekStart + " to " + weekStop ) ;
// Set up the next loop.
weekStart = weekStart.plusWeeks( 1 ) ;
} while ( ! weekStart.isAfter( endOfMonth ) ) ;
See this code run live at IdeOne.com.
Week: Monday, December 31, 2012 to Sunday, January 6, 2013
Week: Monday, January 7, 2013 to Sunday, January 13, 2013
Week: Monday, January 14, 2013 to Sunday, January 20, 2013
Week: Monday, January 21, 2013 to Sunday, January 27, 2013
Week: Monday, January 28, 2013 to Sunday, February 3, 2013
By the way, you might want to learn about, and consider using, the ISO 860 standard definition of a week. And then use the YearWeek
class of the ThreeTen-Extra library.
Upvotes: 3
Reputation: 771
I get the 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;
}
Upvotes: 1
Reputation: 20431
A possible solution may be to not reinvent the wheel and use JodaTime or a similar library. For instance, you can use the dayOfWeek() function on a DateTime
to get the information you're after.
Upvotes: 1