java制作日历


import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

/**
 * @author dj
 * @version 1.0
 **/

public class Main {
    public static void main(String[] args)  {
        Scanner in = new Scanner(System.in);
        //输入你想要的月份的日历,1代表1月
        int month_you_want = in.nextInt();
        //可获得有关星期几的数组
        String[] NameOfWeekdays = new DateFormatSymbols().getShortWeekdays();
        
        GregorianCalendar now = new GregorianCalendar();
        now.setTime(new Date());
        //设置为你想要的月份
        now.set(Calendar.MONTH,month_you_want-1);
        int month = now.get(Calendar.MONTH);
        for (int i = 1;i<=7;i++){
            System.out.print(NameOfWeekdays[i]+"\t");
        }
        System.out.println();
        // find the first day of this month
        //找到该月的第一天是星期几
        now.set(Calendar.DAY_OF_MONTH,1);
        int theFirstDay = now.get(Calendar.DAY_OF_WEEK);
        for (int i = 0;i<theFirstDay-1;i++){
            System.out.print("\t\t");
        }
//        第一行日期容易输出,先输出第一行日期
        for(int i = 1;i<7 - theFirstDay+2;i++){
            System.out.print(i+"\t\t");
        }
        System.out.println();
        //将日期设置为日历第二行的首个日期
        now.set(Calendar.DAY_OF_MONTH,7 - theFirstDay+2);
        //便于接下来,每七个换一行
        int numberOfLine = 0;


        do{
            System.out.print(now.get(Calendar.DAY_OF_MONTH)+"\t\t");
            numberOfLine++;
            if(numberOfLine ==7){
                System.out.println();
                numberOfLine = 0;
            }
            now.add(Calendar.DAY_OF_MONTH,1);
        }while (now.get(Calendar.MONTH) == month);
        //一旦进入下一个月,则程序就终止执行

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿了个猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值