万年历

public class PerpetualCalendar {

    public static void main(String[] args) {
        System.out.println("******************欢迎使用万年历*****************");
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择年份:");
        int year = sc.nextInt();
        Scanner sc1 = new Scanner(System.in);
        System.out.print("请选择月份:");
        int mon = sc1.nextInt();

        // 设置日历对象的年、月、日
        Calendar c = Calendar.getInstance();
        c.set(year, mon, 1);// 输入的后一个月的一号
        c.add(Calendar.DATE, -1);// 这个月的最后一天
        int lastDay = c.get(Calendar.DATE);

        Calendar c1 = Calendar.getInstance();
        c1.set(year, mon - 1, 1);
        System.out.printf("%-6s %-6s %-6s %-6s %-6s %-6s %-6s", "星期日", "星期一","星期二", "星期三", "星期四", "星期五", "星期六");
        System.out.println();
        int i = 1;
        while (i <= lastDay) {
            String s1 = PerpetualCalendar.dateToString(c1.getTime(), "EEE");
            String s2 = PerpetualCalendar.dateToString(c1.getTime(), "dd");
            if (i == 1) {
                if (s1.equals("星期一")) {
                    System.out.printf("%-6s %-7s","",s2);
                } else if (s1.equals("星期二")) {
                    System.out.printf("%-6s %-6s %-7s","","", s2);
                } else if (s1.equals("星期三")) {
                    System.out.printf("%-6s %-6s %-6s %-7s","","","", s2);
                } else if (s1.equals("星期四")) {
                    System.out.printf("%-6s %-6s %-6s %-6s %-7s","","","","", s2);
                } else if (s1.equals("星期五")) {
                    System.out.printf("%-6s %-6s %-6s %-6s %-6s %-7s","","","","","", s2);
                } else if (s1.equals("星期六")) {
                    System.out.printf("%-6s %-6s %-6s %-6s %-6s %-6s %-7s","","","","","","", s2);
                    System.out.println();
                } else if (s1.equals("星期日")) {
                    System.out.printf("%-7s", s2);
                }
            }else if(s1.equals("星期六")){
                System.out.printf("%-7s", s2);
                System.out.println();
            }else{
                System.out.printf("%-7s", s2);
            }
            c1.add(Calendar.DATE, 1);
            i++;
        }
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String s = sdf.format(date);
        return s;

    }

}

 

上一篇:H-大时钟(扩展欧几里得)


下一篇:常用时间处理总结__网上找的时间工具类