import java.util.Calendar;
import java.util.GregorianCalendar;
public class CalendarTest
{
public static void main(String[] args)
{
GregorianCalendar d = new GregorianCalendar();//定义d作为当前日期
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
d.set(Calendar.DAY_OF_MONTH, 1);// 设置当前日期d开始月份
int weekday = d.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun Mon Tue Wed Thu Fri Sat");//打印日历头
for (int i = Calendar.SUNDAY; i < weekday; i++ )//缩排日历
System.out.print(" ");
while (d.get(Calendar.MONTH) == month)
{
int day = d.get(Calendar.DAY_OF_MONTH);// 打印天
if (day < 10)
System.out.print(" ");
System.out.print(day);
if (day == today)// 做当前天的标记 *
System.out.print("* ");
else
System.out.print(" ");
if (weekday == Calendar.SATURDAY)// 逢星期六开始一个新的行
System.out.println();
d.add(Calendar.DAY_OF_MONTH, 1);//增加 d 到下一天
weekday = d.get(Calendar.DAY_OF_WEEK);
}
}
}
Canlendar类使用的一个简单例子
最新推荐文章于 2023-05-08 16:42:27 发布