Angeregt durch den Compact Calendar (http://davidseah.com/compact-calendar/) wollte ich etwas ähnliches in meine Webanwendung einbauen, sodass das Ergebnis tabellenartig wie https://docs.google.com/spreadsheet/ccc?key=0AkyxK00VLnSidEl1SS1sZjZiVlpuRnJIY1JmUW9IRHc#gid=0 formatiert wird.
import java.text.DateFormatSymbols; import java.util.*; public class PrintCalender { public static class CalLine { int weekOfYear; int month = -1; // 0 <= month <= 11 int[] day = { -1, -1, -1, -1, -1, -1, -1 }; } public static List<CalLine> calenderOfTheYear( int year ) { Calendar cal = new GregorianCalendar( year, 1, 1 ); List<CalLine> lines = new ArrayList<>(); CalLine line = new CalLine(); for ( int dayOfYear = 1; dayOfYear <= cal.getActualMaximum( Calendar.DAY_OF_YEAR ); dayOfYear++ ) { cal.set( Calendar.DAY_OF_YEAR, dayOfYear ); line.weekOfYear = cal.get( Calendar.WEEK_OF_YEAR ); // Set several times, thats ok int dayOfMonth = cal.get( Calendar.DAY_OF_MONTH ); int dayOfWeek = cal.get( Calendar.DAY_OF_WEEK ); if ( dayOfMonth == 1 ) line.month = cal.get( Calendar.MONTH ); line.day[dayOfWeek - 1] = dayOfMonth; if ( dayOfWeek == Calendar.SUNDAY ) { lines.add( line ); line = new CalLine(); } } lines.add( line ); return lines; } public static void main( String[] args ) { List<CalLine> lines = calenderOfTheYear( 2012 ); String[] monthNames = new DateFormatSymbols( Locale.GERMANY ).getMonths(); System.out.println( "KW Mo Do Mi Do Fr Sa So" ); // to lazy for DateFormatSymbols here... for ( CalLine l : lines ) { String monthStr = (l.month == -1) ? " " : monthNames[ l.month ].substring( 0, 3 ); // Days are Sun, Mon, ..., Sat. Rearange to Mon, ..., Sun String s = String.format( "%2d %s %(2d %(2d %(2d %(2d %(2d %(2d %(2d", l.weekOfYear, monthStr, l.day[1], l.day[2], l.day[3], l.day[4], l.day[5], l.day[6], l.day[0] ).replace( "(1)", " " ); System.out.println( s ); } } }
Zur Demo gibt eine Textausgabe (mit einem Hack). Das Ergebnis für dieses Jahr:
kw mo do mi do fr sa so 52 jan 1 1 2 3 4 5 6 7 8 2 9 10 11 12 13 14 15 3 16 17 18 19 20 21 22 4 23 24 25 26 27 28 29 5 feb 30 31 1 2 3 4 5 6 6 7 8 9 10 11 12 7 13 14 15 16 17 18 19 8 20 21 22 23 24 25 26 9 mär 27 28 29 1 2 3 4 10 5 6 7 8 9 10 11 11 12 13 14 15 16 17 18 12 19 20 21 22 23 24 25 13 apr 26 27 28 29 30 31 1 14 2 3 4 5 6 7 8 15 9 10 11 12 13 14 15 16 16 17 18 19 20 21 22 17 23 24 25 26 27 28 29 18 mai 30 1 2 3 4 5 6 19 7 8 9 10 11 12 13 20 14 15 16 17 18 19 20 21 21 22 23 24 25 26 27 22 jun 28 29 30 31 1 2 3 23 4 5 6 7 8 9 10 24 11 12 13 14 15 16 17 25 18 19 20 21 22 23 24 26 jul 25 26 27 28 29 30 1 27 2 3 4 5 6 7 8 28 9 10 11 12 13 14 15 29 16 17 18 19 20 21 22 30 23 24 25 26 27 28 29 31 aug 30 31 1 2 3 4 5 32 6 7 8 9 10 11 12 33 13 14 15 16 17 18 19 34 20 21 22 23 24 25 26 35 sep 27 28 29 30 31 1 2 36 3 4 5 6 7 8 9 37 10 11 12 13 14 15 16 38 17 18 19 20 21 22 23 39 24 25 26 27 28 29 30 40 okt 1 2 3 4 5 6 7 41 8 9 10 11 12 13 14 42 15 16 17 18 19 20 21 43 22 23 24 25 26 27 28 44 nov 29 30 31 1 2 3 4 45 5 6 7 8 9 10 11 46 12 13 14 15 16 17 18 47 19 20 21 22 23 24 25 48 dez 26 27 28 29 30 1 2 49 3 4 5 6 7 8 9 50 10 11 12 13 14 15 16 51 17 18 19 20 21 22 23 52 24 25 26 27 28 29 30 1 31
+1 für das Syntaxhightlighting, danke!
Da sind sieben Zeilen abgeschnitten, zumindest in meiner Ansicht. Bitte überprüfen.