//使用GregorianCalendar输出日历
package Test;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("输入年月日");
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
GregorianCalendar data = new GregorianCalendar(year, month - 1, day);//month是在1上加多少
System.out.println("日 一 二 三 四 五 六");
data.set(Calendar.DAY_OF_MONTH, 1); //设置1日为第一天
int weekDay = data.get(Calendar.DAY_OF_WEEK); //获得星期几
//日历第一天开始前面的几个空格
for (int i = Calendar.SUNDAY; i < weekDay; ++i)
{
System.out.print(" ");
}
//输出日历
do
{
int nowDay = data.get(Calendar.DAY_OF_MONTH);
System.out.print(nowDay);
//判断是否为当天,当天加上*号标记
if (nowDay == day)
{
System.out.print("* ");
}
else
{
System.out.print(" ");
}
//为了排版整齐,当日小于10时多加一个空格
if (nowDay < 10)
{
System.out.print(" ");
}
//如果那天为星期六,则开始换行
if (weekDay == Calendar.SATURDAY)
{
System.out.println();
}
data.add(Calendar.DAY_OF_MONTH, 1); //日期加1
weekDay = data.get(Calendar.DAY_OF_WEEK);
}while (data.get(Calendar.MONTH) != month);
}
}
使用GregorianCalendar输出日历
最新推荐文章于 2023-02-15 10:21:51 发布