Java Calendar 日历类的时间操作,这也许是 Java 环境下创建和管理日历及操作时间最简单的一个方案了,示范代码也很简单。
演示了获取时间,日期时间的累加和累减,以及日期时间的比较。
注意事项:
Calendar 的 month 从 0 开始,也就是全年 12 个月由 0 ~ 11 进行表示。
而 Calendar.DAY_OF_WEEK 定义值如下,每周从 SUNDAY 开始,从 1 ~ 7:
Calendar.SUNDAY = 1
Calendar.MONDAY = 2
Calendar.TUESDAY = 3
Calendar.WEDNESDAY = 4
Calendar.THURSDAY = 5
Calendar.FRIDAY = 6
Calendar.SATURDAY = 7
Java Calendar 演示代码如下:
package com.cj.test.calendar; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) { //完整显示今天的日期时间 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));// 2018-01-05 09:04:20 //创建Calendar对象 Calendar calendar = Calendar.getInstance(); //对calendar设置时间的方法 //设置传入的时间格式 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //指定一个日期 Date date = dateFormat.parse("2013-6-1 13:24:16"); System.out.println(date); //Sat Jun 01 13:24:16 CST 2013 //对calendar设置为date指定的日期 calendar.setTime(date); // 按特定格式显示刚设置的时间 String str; str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(calendar.getTime()); System.out.println(str); //2013-06-01 13:24:16:000 } catch (ParseException e) { e.printStackTrace(); } //还有另一种设置的方式 calendar = Calendar.getInstance(); calendar.set(2013, 1, 2, 17, 35, 44); String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(calendar.getTime()); System.out.println(str); //2013-02-02 17:35:44:011 //calendar取得当前时间的方法 calendar.setTime(new Date()); str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()); System.out.println(str); //2018-01-05 09:47:23 //显示年份 int year = calendar.get(Calendar.YEAR); System.out.println("year is = "+year); //year is = 2018 System.out.println("year is = "+String.valueOf(year)); //year is = 2018 //显示月份(从0开始,实际显示要加1) int month = calendar.get(Calendar.MONTH); System.out.println("month is = "+(month+1)); //month is = 1 //本周几 int week = calendar.get(Calendar.DAY_OF_WEEK); //week is = 6 System.out.println("week is = "+week);//今年的第几天 int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR); //DAY_OF_YEAR is = 5 System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR); //本月的第几天 int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH); //DAY_OF_MONTH is = 5 System.out.println("DAY_OF_MONTH is = "+DAY_OF_MONTH); // 3小时以后 calendar.add(Calendar.HOUR_OF_DAY, 3); int HOUR_OF_DAY = calendar.get(Calendar.HOUR_OF_DAY); //HOUR_OF_DAY + 3 = 13 System.out.println("HOUR_OF_DAY + 3 = " + HOUR_OF_DAY); // 当前分钟数 int MINUTE = calendar.get(Calendar.MINUTE); //MINUTE = 7 System.out.println("MINUTE = " + MINUTE); // 15 分钟以后 calendar.add(Calendar.MINUTE, 15); //MINUTE + 15 = 22 MINUTE = calendar.get(Calendar.MINUTE); System.out.println("MINUTE + 15 = " + MINUTE); // 30分钟前 calendar.add(Calendar.MINUTE, -30); MINUTE = calendar.get(Calendar.MINUTE); System.out.println("MINUTE - 30 = " + MINUTE); //MINUTE - 30 = 52 // 创建一个 Calendar 用于比较时间 Calendar calendarNew = Calendar.getInstance(); // 设定为 5 小时以前,后者大,显示 -1 calendarNew.add(Calendar.HOUR, -5); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); //时间比较:-1 // 设定7小时以后,前者大,显示 1 calendarNew.add(Calendar.HOUR, +7); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); //时间比较:-1 // 退回 2 小时,时间相同,显示 0 calendarNew.add(Calendar.HOUR, -2); System.out.println("时间比较:" + calendarNew.compareTo(calendar)); //时间比较:-1 } }
参考资料:
https://blog.csdn.net/joyous/article/details/9630893