Calendar使用

时间:2021-12-23 22:14:29

1简单例子

package com.kungeek.tip;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class Test
{ public static void main(String[] args) throws Exception
{
//字符串转换日期格式
SimpleDateFormat fmtDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//得到日期格式对象
Date setDate = fmtDateTime.parse("2010-03-03 12:12:12");
//日期转换为字符串格式
String setStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(setDate);
System.out.println(setStr); // 完整显示日期时间
String str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(new Date());
System.out.println(str); // 创建 Calendar 对象
Calendar calendar = Calendar.getInstance();
// 初始化 Calendar 对象,但并不必要,除非需要重置时间
calendar.setTime(new Date()); // setTime 类似上面一行
// Date date = new Date();
// calendar.setTime(setDate); // 显示年份
int year = calendar.get(Calendar.YEAR);
System.out.println("YEAR is = " + String.valueOf(year)); // 显示月份 (从0开始, 实际显示要加一)
int MONTH = calendar.get(Calendar.MONTH);
System.out.println("MONTH is = " + (MONTH + 1)); // 今年的第 N 天
int DAY_OF_YEAR = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("DAY_OF_YEAR is = " + DAY_OF_YEAR); // 本月第 N 天
int DAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("DAY_OF_MONTH = " + String.valueOf(DAY_OF_MONTH)); // 3小时以后
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); // 15 分钟以后
calendar.add(Calendar.MINUTE, 15);
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); // 格式化显示
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println("经过一些运算后现在时间是 :"+str); // 重置 Calendar 显示当前时间
calendar.setTime(new Date());
str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
System.out.println("重置 Calendar 显示当前时间 :"+str); // 创建一个 Calendar 用于比较时间
Calendar calendarNew = Calendar.getInstance(); // 设定为 5 小时以前,后者大,显示 -1
calendarNew.add(Calendar.HOUR, -5);
System.out.println("时间比较:" + calendarNew.compareTo(calendar)); // 设定7小时以后,前者大,显示 1
calendarNew.add(Calendar.HOUR, +7);
System.out.println("时间比较:" + calendarNew.compareTo(calendar)); // 退回 2 小时,时间相同,显示 0
calendarNew.add(Calendar.HOUR, -2);
System.out.println("时间比较:" + calendarNew.compareTo(calendar));
}
}

结果输出

2015-10-12 16:13:21:655
YEAR is = 2015
MONTH is = 10
DAY_OF_YEAR is = 285
DAY_OF_MONTH = 12
HOUR_OF_DAY + 3 = 19
MINUTE = 13
MINUTE + 15 = 28
MINUTE - 30 = 58
经过一些运算后现在时间是 :2015-10-12 18:58:21:655
重置 Calendar 显示当前时间 :2015-10-12 16:13:21:655
时间比较:-1
时间比较:1
时间比较:0