import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner; public class VirtualCalendar { private static Scanner scanner; public static void main(String[] args) throws ParseException {
try {
System.out.println("请输入年份(例:2016)");
scanner = new Scanner(System.in);
int year = Integer.parseInt(scanner.nextLine());
printCalendarOfAYear(year);
} catch (ParseException e) {
e.printStackTrace();
} } /**
* 打印星期
*/
public static void printWeekLine() {
System.out.println("日\t一\t二\t三\t四\t五\t六");
} /**
* 打印某一年所有月份的日历
* @param year 年份
* @throws ParseException
*/
public static void printCalendarOfAYear(int year) throws ParseException {
for (int i = 1; i <= 12; i++) {
System.out.println("┍————————————————————————————————┑");
System.out.println("♫\t\t\t" + i + "月" + "\t\t\t ♫");
System.out.println("┕————————————————————————————————┙");
printWeekLine();
printCalendarOfAMonth(year, i); }
} /**
* 打印某年某月的日历
* @param year 年份
* @param month 月份
* @throws ParseException
*/
public static void printCalendarOfAMonth(int year, int month)
throws ParseException {
DateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
String dataString = year + "-" + month + "-" + "01";
Date date = dateFormate.parse(dataString);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i <= days; i++) {
int week = calendar.get(Calendar.DAY_OF_WEEK);
if (i == 1) {
for (int j = 0; j < week - 1; j++) {
System.out.print("*\t");
}
}
System.out.print(i);
if (week == Calendar.SATURDAY) {
System.out.print("\n");
} else {
System.out.print("\t");
}
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println();
System.out.println();
}
}

运行后的结果

请输入年份(例:2016)
2001
┍————————————————————————————————┑
♫ 1月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 ┍————————————————————————————————┑
♫ 2月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 ┍————————————————————————————————┑
♫ 3月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31 ┍————————————————————————————————┑
♫ 4月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 ┍————————————————————————————————┑
♫ 5月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 ┍————————————————————————————————┑
♫ 6月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * * 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30 ┍————————————————————————————————┑
♫ 7月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 ┍————————————————————————————————┑
♫ 8月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 ┍————————————————————————————————┑
♫ 9月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * * * 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 ┍————————————————————————————————┑
♫ 10月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 ┍————————————————————————————————┑
♫ 11月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 ┍————————————————————————————————┑
♫ 12月 ♫
┕————————————————————————————————┙
日 一 二 三 四 五 六
* * * * * * 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

此示例主要使用Calendar 类及DataFormate列的子类的使用

最新文章

  1. 【Windows编程】系列第五篇:GDI图形绘制
  2. 嵌入式Linux驱动学习之路(十五)按键驱动-定时器防抖
  3. EditText添加了ImageSpan后,在两者中间不能输入纯文本
  4. android异常: java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused
  5. three.js学习笔记
  6. 基于asp.net+MINIUI的项目----在线学习系统
  7. cvLoadImage
  8. AWK处理日志入门(转)
  9. 关于c语言char类型输入输出的一个bug
  10. [转]SpringMVC日期类型转换问题三大处理方法归纳
  11. SQLServer 错误: 15404,无法获取有关 Windows NT 组 用户
  12. JSP中文编码问题
  13. Leetcode题解(一)
  14. python Django2.X,报错 ‘learning_logs ’is not a registered namespace,如何解决?
  15. 关于Java面试
  16. nvm 知识点
  17. skywalking部署
  18. 一个基于typelist的typemap
  19. Matplotlib:tick_params参数设置
  20. 使用Wisdom RESTClient自动化测试REST API,如何取消对返回的body内容的校验?

热门文章

  1. EF 在controller弹出提示消息
  2. 关于 .NET Core 动态链接库的开发
  3. ef
  4. IntelliJ IDEA使用(二):tomcat和jetty配置
  5. CSS布局 -- 圣杯布局 &amp; 双飞翼布局
  6. Android开发学习—— Service 服务
  7. ios native工程集成react-native的demo
  8. UICollectionViewCell--查找cell上的按钮点击后,对应的是哪个cell
  9. MAC OS X El CAPITAN 搭建SPRING MVC (1)- 目录、包名、创建web.xml
  10. 【代码笔记】iOS-自定义导航条颜色