Java中的GregorianCalendar类


GregorianCalendar 是 Calendar 类的一个具体实现,它实现了您熟悉的标准格里高利历。在本教程中我们没有讨论 Calendar 类,您可以查阅标准 Java 文档。

Calendar 的 getInstance() 方法返回一个 GregorianCalendar 对象,该对象使用默认区域设置和时区中的当前日期和时间进行初始化。GregorianCalendar 定义了两个字段:AD 和 BC。它们表示格里高利历定义的两个纪元。

GregorianCalendar 对象还有几个构造函数:

序号
构造函数及描述
1
GregorianCalendar()
使用默认时区和默认区域设置中的当前时间构造一个默认的 GregorianCalendar。

2
GregorianCalendar(int year, int month, int date)
使用默认时区和默认区域设置中给定的日期构造一个 GregorianCalendar。

3
GregorianCalendar(int year, int month, int date, int hour, int minute)
使用默认时区和默认区域设置中给定的日期和时间构造一个 GregorianCalendar。

4
GregorianCalendar(int year, int month, int date, int hour, int minute, int second)
使用默认时区和默认区域设置中给定的日期和时间构造一个 GregorianCalendar。

5
GregorianCalendar(Locale aLocale)
基于默认时区和给定区域设置中的当前时间构造一个 GregorianCalendar。

6
GregorianCalendar(TimeZone zone)
基于给定时区和默认区域设置中的当前时间构造一个 GregorianCalendar。

7
GregorianCalendar(TimeZone zone, Locale aLocale)
基于给定时区和给定区域设置中的当前时间构造一个 GregorianCalendar。

以下是 GregorianCalendar 类提供的一些有用的支持方法:

序号
方法及描述
1
void add(int field, int amount)
根据日历规则,将指定的时间量添加到给定的时间字段。

2
protected void computeFields()
将 UTC 毫秒转换为时间字段值。

3
protected void computeTime()
覆盖 Calendar,将时间字段值转换为 UTC 毫秒。

4
boolean equals(Object obj)
将此 GregorianCalendar 与对象引用进行比较。

5
int get(int field)
获取给定时间字段的值。

6
int getActualMaximum(int field)
返回给定当前日期,此字段可能具有的最大值。

7
int getActualMinimum(int field)
返回给定当前日期,此字段可能具有的最小值。

8
int getGreatestMinimum(int field)
如果给定字段的值有所不同,则返回该字段的最高最小值。

9
Date getGregorianChange()
获取格里高利历更改日期。

10
int getLeastMaximum(int field)
如果给定字段的值有所不同,则返回该字段的最低最大值。

11
int getMaximum(int field)
返回给定字段的最大值。

12
Date getTime()
获取此日历的当前时间。

13
long getTimeInMillis()
获取此日历的当前时间(长整型)。

14
TimeZone getTimeZone()
获取时区。

15
int getMinimum(int field)
返回给定字段的最小值。

16
int hashCode()
覆盖 hashCode。

17
boolean isLeapYear(int year)
确定给定年份是否是闰年。

18
void roll(int field, boolean up)
在给定的时间字段上增加或减去(向上/向下)一个时间单位,而不更改较大的字段。

19
void set(int field, int value)
使用给定值设置时间字段。

20
void set(int year, int month, int date)
设置 year、month 和 date 字段的值。

21
void set(int year, int month, int date, int hour, int minute)
设置 year、month、date、hour 和 minute 字段的值。

22
void set(int year, int month, int date, int hour, int minute, int second)
设置 year、month、date、hour、minute 和 second 字段的值。

23
void setGregorianChange(Date date)
设置格里高利历更改日期。

24
void setTime(Date date)
使用给定的日期设置此日历的当前时间。

25
void setTimeInMillis(long millis)
使用给定的长整型值设置此日历的当前时间。

26
void setTimeZone(TimeZone value)
使用给定的时区值设置时区。

27
String toString()
返回此日历的字符串表示形式。

示例

在线演示

import java.util.*;
public class GregorianCalendarDemo {

   public static void main(String args[]) {
      String months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

      int year;
      // Create a Gregorian calendar initialized
      // with the current date and time in the
      // default locale and timezone.

      GregorianCalendar gcalendar = new GregorianCalendar();

      // Display current time and date information.
      System.out.print("Date: ");
      System.out.print(months[gcalendar.get(Calendar.MONTH)]);
      System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
      System.out.println(year = gcalendar.get(Calendar.YEAR));
      System.out.print("Time: ");
      System.out.print(gcalendar.get(Calendar.HOUR) + ":");
      System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
      System.out.println(gcalendar.get(Calendar.SECOND));

      // Test if the current year is a leap year
      if(gcalendar.isLeapYear(year)) {
         System.out.println("The current year is a leap year");
      }else {
         System.out.println("The current year is not a leap year");
      }
   }
}

这将产生以下结果:

输出

Date: Apr 22 2009
Time: 11:25:27
The current year is not a leap year

更新于:2020-06-19

浏览量 185 次

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.