Java Calendar类的详解及使用实例

2025-05-29 0 72

Java Calendar的使用总结

  在实际项目当中,我们经常会涉及到对时间的处理,例如登陆网站,我们会看到网站首页显示XXX,欢迎您!今天是XXXX年。。。。某些网站会记录下用户登陆的时间,比如银行的一些网站,对于这些经常需要处理的问题,Java中提供了Calendar这个专门用于对日期进行操作的,那么这个有什么特殊的地方呢,首先我们来看Calendar的声明

?

1
public abstract class Calendar extends Objectimplements Serializable, Cloneable, Comparable<Calendar>

  该被abstract所修饰,说明不能通过new的方式来获得实例,对此,Calendar提供了一个方法getInstance,以获得此型的一个通用的对象,getInstance方法返回一个Calendar对象(该对象为Calendar的子对象),其日历字段已由当前日期和时间初始化:

?

1
Calendar rightNow = Calendar.getInstance();

  为什么说返回的是Calendar的子对象呢,因为每个国家地区都有自己的一套日历算法,比如西方国家的第一个星期大部分为星期日,而中国则为星期一,我们来看看getInstance方法获取实例的源码

?

1

2

3

4

5

6

7

8

9

10

11

12

13
/**

* Gets a calendar using the default time zone and locale. The

* <code>Calendar</code> returned is based on the current time

* in the default time zone with the default locale.

*

* @return a Calendar.

*/

public static Calendar getInstance()

{

Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));

cal.sharedZone = true;

return cal;

}

其中createCalendar方法就是根据不同国家地区返回对应的日期子

?

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
private static Calendar createCalendar(TimeZone zone,

Locale aLocale)

{

Calendar cal = null;

String caltype = aLocale.getUnicodeLocaleType("ca");

if (caltype == null) {

// Calendar type is not specified.

// If the specified locale is a Thai locale,

// returns a BuddhistCalendar instance.

if ("th".equals(aLocale.getLanguage())

&& ("TH".equals(aLocale.getCountry()))) {

cal = new BuddhistCalendar(zone, aLocale);

} else {

cal = new GregorianCalendar(zone, aLocale);

}

} else if (caltype.equals("japanese")) {

cal = new JapaneseImperialCalendar(zone, aLocale);

} else if (caltype.equals("buddhist")) {

cal = new BuddhistCalendar(zone, aLocale);

} else {

// Unsupported calendar type.

// Use Gregorian calendar as a fallback.

cal = new GregorianCalendar(zone, aLocale);

}

return cal;

}

  为了更加便捷的对日期进行操作,Calendar对YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量。

下面看看Calendar常用的方法

?

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

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98
package com.test.calendar;

import java.util.Calendar;

import org.junit.Before;

import org.junit.Test;

public class CalendarDemo {

Calendar calendar = null;

@Before

public void test() {

calendar = Calendar.getInstance();

}

// 基本用法,获取年月日时分秒星期

@Test

public void test1() {

// 获取年

int year = calendar.get(Calendar.YEAR);

// 获取月,这里需要需要月份的范围为0~11,因此获取月份的时候需要+1才是当前月份值

int month = calendar.get(Calendar.MONTH) + 1;

// 获取日

int day = calendar.get(Calendar.DAY_OF_MONTH);

// 获取时

int hour = calendar.get(Calendar.HOUR);

// int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时表示

// 获取分

int minute = calendar.get(Calendar.MINUTE);

// 获取秒

int second = calendar.get(Calendar.SECOND);

// 星期,英语国家星期从星期日开始计算

int weekday = calendar.get(Calendar.DAY_OF_WEEK);

System.out.println("现在是" + year + "年" + month + "月" + day + "日" + hour

+ "时" + minute + "分" + second + "秒" + "星期" + weekday);

}

// 一年后的今天

@Test

public void test2() {

// 同理换成下个月的今天calendar.add(Calendar.MONTH, 1);

calendar.add(Calendar.YEAR, 1);

// 获取年

int year = calendar.get(Calendar.YEAR);

// 获取月

int month = calendar.get(Calendar.MONTH) + 1;

// 获取日

int day = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println("一年后的今天:" + year + "年" + month + "月" + day + "日");

}

// 获取任意一个月的最后一天

@Test

public void test3() {

// 假设求6月的最后一天

int currentMonth = 6;

// 先求出7月份的第一天,实际中这里6为外部传递进来的currentMonth变量

// 1

calendar.set(calendar.get(Calendar.YEAR), currentMonth, 1);

calendar.add(Calendar.DATE, -1);

// 获取日

int day = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println("6月份的最后一天为" + day + "号");

}

// 设置日期

@Test

public void test4() {

calendar.set(Calendar.YEAR, 2000);

System.out.println("现在是" + calendar.get(Calendar.YEAR) + "年");

calendar.set(2008, 8, 8);

// 获取年

int year = calendar.get(Calendar.YEAR);

// 获取月

int month = calendar.get(Calendar.MONTH);

// 获取日

int day = calendar.get(Calendar.DAY_OF_MONTH);

System.out.println("现在是" + year + "年" + month + "月" + day + "日");

}

}

程序输出结果:

?

1

2

3

4

5
现在是2016117114218秒星期2

一年后的今天:2017117

6月份的最后一天为30

现在是2000

现在是200888

Calendar中也有before,after,compareTo等方法,用法与Date似,只是现在推荐用Calendar操作日期。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.cnblogs.com/huangminwen/p/6041168.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java Calendar类的详解及使用实例 https://www.kuaiidc.com/116950.html

相关文章

发表评论
暂无评论