Java Calendar 类时间操作,这也许是创建和管理日历最简单的一个方案,示范代码很简单。
演示了获取时间,日期时间的累加和累减,以及比较。
注意事项:
Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。
而 Calendar.DAY_OF_WEEK 定义和值如下:
Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7
SimpleDateFormat 的格式定义
Java Calendar 演示代码如下:
- package demo;
-
- import java.util.Date;
- import java.text.SimpleDateFormat;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.util.Calendar;
-
- public class Test
- {
- public Test()
- {
- }
-
- public static void main(String[] args)
- {
-
-
-
-
-
-
-
-
- String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
- System.out.println(str);
-
-
- Calendar calendar = Calendar.getInstance();
-
- try
- {
-
-
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
-
- Date date = dateFormat.parse("2013-6-1 13:24:16");
-
- calendar.setTime(date);
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
- }
- catch (ParseException e)
- {
- e.printStackTrace();
- }
-
-
-
- calendar = Calendar.getInstance();
- calendar.set(2013, 1, 2, 17, 35, 44);
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
-
-
-
- calendar = Calendar.getInstance();
-
- calendar.setTime(new Date());
-
-
-
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime());
- System.out.println(str);
-
-
- int year = calendar.get(Calendar.YEAR);
- System.out.println("year is = " + String.valueOf(year));
-
-
- int month = calendar.get(Calendar.MONTH);
- System.out.println("nth is = " + (month + 1));
-
-
- int week = calendar.get(Calendar.DAY_OF_WEEK);
- System.out.println("week is = " + week);
-
-
- int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
- System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR);
-
-
- int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
- System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH));
-
-
- calendar.add(Calendar.HOUR_OF_DAY, 3);
- int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY);
- System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY);
-
-
- int MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE = " + MINUTE);
-
-
- calendar.add(Calendar.MINUTE, 15);
- MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE + 15 = " + MINUTE);
-
-
- calendar.add(Calendar.MINUTE, -30);
- MINUTE = calendar.get(Calendar.MINUTE);
- System.out.println("MINUTE - 30 = " + MINUTE);
-
-
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
- System.out.println(str);
-
-
- calendar.setTime(new Date());
- str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
- System.out.println(str);
-
-
- Calendar calendarNew = Calendar.getInstance();
-
-
- calendarNew.add(Calendar.HOUR, -5);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
-
-
- calendarNew.add(Calendar.HOUR, +7);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
-
-
- calendarNew.add(Calendar.HOUR, -2);
- System.out.println("时间比较:" + calendarNew.compareTo(calendar));
- }
- }
要计算时间差,可用 Calendar.getTimeInMillis() 取得两个时间的微秒级的时间差,再加以换算即可,比如获得相差天数,代码如下:
-
- long val = calendarEnd.getTimeInMillis() - calendarBegin.getTimeInMillis();
-
- long day = val / (1000 * 60 * 60 * 24);
问题:输入年,月,打印对应年月的日历。
示例:
-------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/aca6e072ca5938e1dfc8e08c640261dd.png)
------------------------------------------------------------------------------------------------------------------
问题分析:
1,首先1970年是Unix系统诞生的时间,1970年成为Unix的元年,1970年1月1号是星期四,现在大多的手机的日历功能只能显示到1970年1月1日这一天;
2,要想打印某年某月的日历,首先应该计算出这个月1号是星期几?
解决1号是星期几?
2.1: 先计算出年天数,即截至这一年1月1号的天数,用for循环,从1970年开始,闰年+366,平年+365;
2.2: 计算出月天数,即截至本月1号的天数,用for循环,从1月份开始,算出月天数;
2.1.1 :用到闰年判断,用switch来判断返回月天数;
2.3: 用年天数加月天数,求得本月1号距离1970年1月1号的总天数,用总天数来判断本月1号是星期几;
3, 判断本月的总天数(用到switch语句);
4, 打印日历;
代码实现:
一个函数实现打印printTitle();即实现固定的头部部分;
实现print(参数1,参数2,参数3);即实现打印日历主题部分;
参数1(计算总天数):计算年天数,计算月天数;
参数2 传递年份;
参数3 传递月份;
判断闰年设置独立函数;
总结:
解决问题前一定要先分析问题,把大问题逐步分成小问题,理清逻辑,实现小问题的逻辑,整个问题就已经实现了百分之八十;
自下而上,逐步实现,函数独立调用;
代码实现部分:
-----------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/58a1bef40c7840b1f7429e3f9edef1b2.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/554358b7330bc3e8069ec900c22c565e.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/819f18ae5f2f1f43777c66e347a65f7b.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/d2df52c674f0359e49479f59dfb42591.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/91ec46b49c9c7f01d13407a3679a0106.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/4e139d405170e0b7d070ec9e137abb6e.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/83f7d7b0c741450535fa743fb2ad81df.png)
------------------------------------------------------------------------------------------------------
![](https://i-blog.csdnimg.cn/blog_migrate/6600d96cb6ed0a24e650b8540deeded7.png)