Java: Créer un calendrier sur le console

Author:


Download

import java.util.*;
import java.text.*;
 
 
public class ConsoleCalendar {
 
 
 
 
    public static void main(String[] argv) 
    {
 
        int mois, annee;
        ConsoleCalendar cp = new ConsoleCalendar( );
        Calendar c = Calendar.getInstance( );
        cp.print(c.get(Calendar.MONTH), c.get(Calendar.YEAR));
 
    }
 
 
    public void print(int mm, int aa) {
 
 
        int leadGap = 0;
 
        System.out.print(mois[mm]);   
        System.out.print(" ");
        System.out.print(aa);
        System.out.println( );
        if (mm < 0 || mm > 11)
 
            throw new IllegalArgumentException("Month " + mm + " bad, must be 0-11");
 
        GregorianCalendar calendar = new GregorianCalendar(aa, mm, 1);
        System.out.printf("%2s%3s%3.5s%3.65s%3.75s%3.9s%3.95sn","L", "M", "M", "J", "V", "S", "D");
 
        leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;
        int daysInMonth = days[mm];
        if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) &#038;& mm == 1)
            ++daysInMonth;
 
 
        for (int i = 0; i < leadGap; i++) {
 
            System.out.print("   ");
 
        }
 
 
        for (int i = 1; i <= daysInMonth; i++) {
 
            if (i<=9)
 
                System.out.print(' ');
 
            System.out.print(i);
 
 
 
            if ((leadGap + i) % 7 == 0)       
 
                System.out.println( );
 
            else
 
                System.out.print(' ');
 
        }
 
        System.out.println( );
 
    }
 
    /** les noms des mois */
 
    String[] mois = {
 
        "Janvier", "Février", "Mars", "Avril",
 
        "Mai", "Juin", "Juillet", "Août",
 
        "Septembre", "Octobre", "Novembre", "Decembre"
 
    };
 
    /** Les jours des mois. */
 
    public final static int[] days = {
 
            31, 28, 31, 30,    
 
            31, 30, 31, 31, 
 
            30, 31, 30, 31   
 
    };
 
 
}