好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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 日的 0.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 现在是2016年11月7日11时42分18秒星期2
2 一年后的今天:2017年11月7日
3 6月份的最后一天为30号
4 现在是2000年
5 现在是2008年8月8日

calendar类中也有before,after,compareto等方法,用法与date类的类似,只是现在推荐用calendar类操作日期

以上所述是小编给大家介绍的java calendar类的使用总结实例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://HdhCmsTestcnblogs测试数据/huangminwen/p/6041168.html

查看更多关于Java Calendar类的使用总结实例的详细内容...

  阅读:11次