Calendar类的使用

Calendar类简介

Calendar 类是一个抽象类,所以不能使用构造器来创建Calendar对象。但它提供了几个静态getInstance()方法来获取Calendar对象这些方法根据 TimeZone,Locale 类来获取特定的Calendar,如果不指定TimeZone、Locale,则使用默认的TimeZone、Locale来创建Calendar 。

private static Calendar createCalendar(TimeZone zone,
				   Locale aLocale)
{
// If the specified locale is a Thai locale, returns a BuddhistCalendar
// instance.
if ("th".equals(aLocale.getLanguage())
    && ("TH".equals(aLocale.getCountry()))) {
    return new sun.util.BuddhistCalendar(zone, aLocale);
} else if ("JP".equals(aLocale.getVariant())
	   && "JP".equals(aLocale.getCountry())
	   && "ja".equals(aLocale.getLanguage())) {
    return new JapaneseImperialCalendar(zone, aLocale);
}	    

// else create the default calendar
    return new GregorianCalendar(zone, aLocale);	
}

中国区的aLocale.getLanguage() == "ZH",由上面的JDK源码可以看出,我们调用getInstance()方法创建的是GregorianCalendar对象。

所以

Calendar c = Calendar.getInstance();
Calendar c1 = new GregorianCalendar();

两种方式创建的都是GregorianCalendar对象。

Calendar类常用方法

Calendar 类提供了大量访问、修改日期时间的方法,常用方法如下 。
void add(int field, int amount) : 根据日历的规则,为给定的日历字段添加或减去指定的时间量 。
int get(int field): 返回指定日历字段的值 。
int getActualMaximum(int field): 返回指定日历字段可能拥有的最大值。 例如月,最大值为11 。
int getActualMinimum(int field): 返回指定日历字段可能拥有的最小值。例如月,最小值为 0 。
void roll(int field, int amount): 与 add() 方法类似,区别在于加上amount 后超过了该字段所能表
示的最大范围时 ,也不会向上一个字段进位 。
void set(int field, int value): 将给定 的 日历字段设置为给定值 。
void set(int year, int month, int date): 设置 Calendar 对象的年、月、日 三个字段 的值 。
void set(int year, int month, int date, int hourOfDay, int minute, int second):设置Calendar对象的年、月、日、时、分、秒6个字段的值 。

roll() 的规则与 add() 的处理规则不同 : 当被修改的字段超出它允许的范围时,上一级字段不会增大。做运算的字段按照原规则变化:

public static void main(String[] args) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, 2019);
		c.set(Calendar.MONTH, 2);//设置为3月
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));//设置3月的最后一天
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

		Calendar c1 = Calendar.getInstance();
		c1.setTime(c.getTime());//c1的时间设置为与c相同
		
		System.out.println("c 的初始时间:" + sdf.format(c.getTime()));
		c.add(Calendar.DAY_OF_MONTH, 1);
		System.out.println("c add之后的时间:" + sdf.format(c.getTime()));
		
		System.out.println("c1 的初始时间:" + sdf.format(c1.getTime()));
		c1.roll(Calendar.DAY_OF_MONTH, 1);
		System.out.println("c1 roll之后的时间" + sdf.format(c1.getTime()));
}

输出:

c 的初始时间:2019/03/31 01:09:39
c add之后的时间:2019/04/01 01:09:39
c1 的初始时间:2019/03/31 01:09:39
c1 roll之后的时间2019/03/01 01:09:39

其中Calendar类中月份从0开始,所以调用set方法来设置月份的时候需要减1。

Java 8 新增的日期、时间包

常用方法:

public static void main(String[] args) {
		//下面是关于 Clock 的用法
		// 获取当前 Clock
		Clock clock = Clock.systemUTC();
		// 通过 Clock 获取当前时刻
		System.out.println(" 当前时刻为: " + clock.instant());
		// 获取 clock 对应的毫秒数,与 System.currentTimeMillis () 输出相同
		System.out.println(clock.millis());
		System.out.println(System.currentTimeMillis());
		//下而是关于 Duration 的用法
		Duration d = Duration.ofSeconds(6000);
		System.out.println(" 6000 秒相当子" + d.toMinutes() + " 分");
		System.out.println(" 6000 秒相当于 " + d.toHours() + " 小时 ");
		System.out.println(" 6000 秒相当于 " + d.toDays() + "天");
		// 在 clock 基础上增加 6000 秒,返回新的 Clock
		Clock clock2 = Clock.offset(clock, d);
		// 可以看到 clock2 与 clockl 相差 1 小时 40 分
		System.out.println(" 当前时刻加 6000 秒为: " + clock2.instant());
		//下面是关于 Instant 的用法
		// 获取当前时间
		Instant instant = Instant.now();
		System.out.println(instant);
		// instant 添加 6000 秒(即 100 分钟) . 返回新的 Instant
		Instant instant2 = instant.plusSeconds(6000);
		System.out.println(instant2);
		// 根据字符串解析 Instant 对象
		Instant instant3 = Instant.parse("2019-02-23T10:12:35.342Z");
		System.out.println(instant3);
		// 在 instant3 的基础上添加 5 小时 4 分钟
		Instant instant4 = instant3.plus(Duration.ofHours(5).plusMinutes(4));
		System.out.println(instant4);
		// 获取 instant4 的 5 天以前的时刻
		Instant instant5 = instant4.minus(Duration.ofDays(5));
		System.out.println(instant5);
		//下面是关于 LocalDate 的用法
		LocalDate localDate = LocalDate.now();
		System.out.println(localDate);
		// 获得 2019 年的第 146 天
		localDate = LocalDate.ofYearDay(2019, 146);
		System.out.println(localDate); // 2019 - 05-26
		// 设置为 2019 年 5 月 21 日
		localDate = LocalDate.of(2019, Month.MAY, 21);
		System.out.println(localDate); // 2019-05 - 21
		//下面是关于 LocalTime 的用法
		// 获取当前时 间
		LocalTime localTime = LocalTime.now();
		// 设置为 22 点 33 分
		localTime = localTime.of(22, 33);
		System.out.println(localTime); // 22 : 33
		// 返回一天中的第 5503秒
		localTime = LocalTime.ofSecondOfDay(5503);
		System.out.println(localTime); // 01 : 31 : 43
		//下面是关于 localDateTime 的用法
		// 获取当前日期、时间
		LocalDateTime localDateTime = LocalDateTime.now();
		// 当前日期 、时间加上 25 小时 3 分钟
		LocalDateTime future = localDateTime.plusHours(25).plusMinutes(3);
		System.out.println(" 当前日期、时间的 25 小时 3 分之后 : " + future);
		//下面是关于 Year、YearMonth,MonthDay 的用法示例
		Year year = Year.now(); // 获取当前的年份
		System.out.println(" 当前年份: " + year); // 输出当前年份
		year = year.plusYears(5); // 当前年份再加 5 年
		System.out.println(" 当前年份再过 5 年: " + year);
		// 根据指定月份获取 YearMonth
		YearMonth ym = year.atMonth(10);
		System.out.println(" year 年 10 月 : " + ym); // 输出 XXXX-10 , XXXX 代表当前年份
		// 当前年月再加 5 年、减 3 个月
		ym = ym.plusYears(5).minusMonths(3);
		System.out.println(" year 年 10 月再加 5 年 、减 3 个月: " + ym);
		MonthDay md = MonthDay.now();
		System.out.println(" 当前月日 : " + md); // 输出 --XX-XX ,代表几月几日
		// 设置为 5 月 23 日
		MonthDay md2 = md.with(Month.MAY).withDayOfMonth(23);
		System.out.println(" 5 月 23 日为 : " + md2); // 输出 --05-23
	}

输出:

当前时刻为: 2019-12-03T06:03:49.102Z
1575353029187
1575353029187
6000 秒相当子100 分
6000 秒相当于 1 小时
6000 秒相当于 0天
当前时刻加 6000 秒为: 2019-12-03T07:43:49.187Z
2019-12-03T06:03:49.187Z
2019-12-03T07:43:49.187Z
2019-02-23T10:12:35.342Z
2019-02-23T15:16:35.342Z
2019-02-18T15:16:35.342Z
2019-12-03
2019-05-26
2019-05-21
22:33
01:31:43
当前日期、时间的 25 小时 3 分之后 : 2019-12-04T15:06:49.209
当前年份: 2019
当前年份再过 5 年: 2024
year 年 10 月 : 2024-10
year 年 10 月再加 5 年 、减 3 个月: 2029-07
当前月日 : --12-03
5 月 23 日为 : --05-23

posted @ 2019-12-03 14:08  猫总会有哒丶  阅读(441)  评论(0编辑  收藏  举报