How to Get a Particular Day List in a Month using Java

Get a Particular Day List in Java

In this example, We will show you about, How to get a particular day list in a month using java

1) Getting Day List from Specific Day

private static List<Date> getSpecificDay(int year, int month, int specificDay) {

	List<Date> dateList = new ArrayList<Date>();

	Calendar calendar = Calendar.getInstance();

	// The month is 0-based in calendar
	calendar.set(year, month - 1, 1);

	int totalDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

	for (int day = 1; day <= totalDaysInMonth; day++) {

		calendar.set(year, month - 1, day);

		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

		if (dayOfWeek == specificDay) {

			dateList.add(calendar.getTime());
		}
	}

	return dateList;
}

2) Formatting the Date

private static String formatDate(Date date) {

	// you can change the format accordingly
	final String format = "d MMM, yyyy";

	DateFormat df = new SimpleDateFormat(format);

	return df.format(date);
}

3) Getting the day name

private static String getDay(int day) {
	
	switch(day) {
	
		case 1:
			return "Sunday";
		case 2:
			return "Monday";
		case 3:
			return "Tuesday";
		case 4:
			return "Wednesday";
		case 5:
			return "Thursday";
		case 6:
			return "Friday";
		case 7:
			return "Saturday";
	}
	
	return "NA";
}

4) Final Example

package com.dineshkrish.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class GetDays {

	private static List<Date> getSpecificDay(int year, int month,
			int specificDay) {

		List<Date> dateList = new ArrayList<Date>();

		Calendar calendar = Calendar.getInstance();

		// The month is 0-based in calendar
		calendar.set(year, month - 1, 1);

		int totalDaysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		for (int day = 1; day <= totalDaysInMonth; day++) {

			calendar.set(year, month - 1, day);

			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

			if (dayOfWeek == specificDay) {

				dateList.add(calendar.getTime());
			}
		}

		return dateList;
	}

	private static String formatDate(Date date) {

		// you can change the format accordingly
		final String format = "d MMM, yyyy";

		DateFormat df = new SimpleDateFormat(format);

		return df.format(date);
	}
	
	private static String getDay(int day) {
		
		switch(day) {
		
			case 1:
				return "Sunday";
			case 2:
				return "Monday";
			case 3:
				return "Tuesday";
			case 4:
				return "Wednesday";
			case 5:
				return "Thursday";
			case 6:
				return "Friday";
			case 7:
				return "Saturday";
		}
		
		return "NA";
	}

	public static void main(String[] args) {

		// Note : 1 - 7 (ie: Sunday - Saturday)
		int[] days = { 1, 2, 3, 4, 5, 6, 7 };
		
		int month = 1;
				
		int year = 2017;

		for (int i = 0; i < days.length; i++) {

			List<Date> dateList = getSpecificDay(year, month, days[i]);

			System.out.println(getDay(days[i]));
			
			System.out.println("--------------------------");
			
			for (Date date : dateList) {

				System.out.println(formatDate(date));
			}
			
			System.out.println();
		}
	}
}

Output

This following output will give you idea about how to get a particular day list using Java.

Sunday
————————–
1 Jan, 2017
8 Jan, 2017
15 Jan, 2017
22 Jan, 2017
29 Jan, 2017

Monday
————————–
2 Jan, 2017
9 Jan, 2017
16 Jan, 2017
23 Jan, 2017
30 Jan, 2017

Tuesday
————————–
3 Jan, 2017
10 Jan, 2017
17 Jan, 2017
24 Jan, 2017
31 Jan, 2017

Wednesday
————————–
4 Jan, 2017
11 Jan, 2017
18 Jan, 2017
25 Jan, 2017

Thursday
————————–
5 Jan, 2017
12 Jan, 2017
19 Jan, 2017
26 Jan, 2017

Friday
————————–
6 Jan, 2017
13 Jan, 2017
20 Jan, 2017
27 Jan, 2017

Saturday
————————–
7 Jan, 2017
14 Jan, 2017
21 Jan, 2017
28 Jan, 2017

References

1. Java Calendar API
2. Java Date API

Hello everyone, I am a Founder of dineshkrish.com.