Jul 27

The Calendar class can be used to generate a simple calendar display.

private static void showMonth(Calendar cal) {
	int month = cal.get(Calendar.MONTH);
	int firstDayOfWeek = cal.getFirstDayOfWeek();
	
	// Display day names as headers
	
	cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
	for (int i=0; i<7; i++) {
		
		System.out.print(cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault()));
		System.out.print(" ");
		cal.add(Calendar.DATE, 1);
	}
	System.out.println();
	
	// Display dates in month
	
	cal.set(Calendar.DATE, cal.getMinimum(Calendar.DATE));
	
	// Now display the dates, one week per line
	
	StringBuilder week = new StringBuilder();
	
	while (month==cal.get(Calendar.MONTH)) {
		
		// Display date
		
		week.append(String.format("%3d ", cal.get(Calendar.DATE)));
					
		// Increment date
		
		cal.add(Calendar.DATE, 1);

		// Check if week needs to be printed
		
		if (cal.get(Calendar.MONTH)!=month) {
			
			// end of month
			// just need to output the month
			
			System.out.println(week);
			
		} else if (cal.get(Calendar.DAY_OF_WEEK)==firstDayOfWeek) {
			
			// new week so print out the current week
			// first check if any padding needed
			
			int padding = 28-week.length();
			if (padding>0) {
				
				// pad out start of week
				
				week.insert(0, 
                                    String.format("%"+padding+"s", " "));
			}
			System.out.println(week);
			week.setLength(0);
		}
	}
}

written by objects \\ tags: , ,