代码:
- package test;
- import java.util.*;
- import java.text.DateFormatSymbols;
- /**
- * @author Demomiku E-mail: 448902872@qq.com
- * @date 创建时间:2015年5月3日 下午8:28:14
- * @version 1.0
- */
- public class MyCalendar
- {
- public static void main(String[] args)
- {
-
- GregorianCalendar d = new GregorianCalendar();
-
- //获得今天是这几月的第几天,这个月是几月,获得的月份是实际月份-1,因为从0开始
- int today = d.get(Calendar.DAY_OF_MONTH);
- int month = d.get(Calendar.MONTH);
- //设置日期为这个月第一天
- d.set(Calendar.DAY_OF_MONTH, 1);
-
- //获取第一天是一个星期的第几天
- int weekday = d.get(Calendar.DAY_OF_WEEK);
- //获得一个星期的第一天
- int firstDayOfWeek = d.getFirstDayOfWeek();
- //日历开头空格的计数
- int indent = 0;
- while (weekday != firstDayOfWeek)
- {
- indent++;
- d.add(Calendar.DAY_OF_MONTH, -1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
- }
- //日历第一行输出星期的缩写
- String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
- do
- {
- System.out.printf("%4s", weekdayNames[weekday]);
- d.add(Calendar.DAY_OF_MONTH, 1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
- }
- while (weekday != firstDayOfWeek);
- System.out.println();
- for (int i = 1; i <= indent; i++)
- System.out.print(" ");
- d.set(Calendar.DAY_OF_MONTH, 1);
- do
- {
- //打印日期
- int day = d.get(Calendar.DAY_OF_MONTH);
- System.out.printf("%3d", day);
- //在今天的后面加上"*"
- if (day == today) System.out.print("*");
- else System.out.print(" ");
- // 将日期+1
- d.add(Calendar.DAY_OF_MONTH, 1);
- weekday = d.get(Calendar.DAY_OF_WEEK);
- // 如果这天是一个星期的第一天则换行
- if (weekday == firstDayOfWeek) System.out.println();
- }
- while (d.get(Calendar.MONTH) == month);
- // 如果判断到是下个月了则结束循环
-
- if (weekday != firstDayOfWeek) System.out.println();
- }
- }
复制代码
截图:
|
-
1.PNG
(2.57 KB, 下载次数: 9)
日历
|