先決條件:java.util.Locale,java.util.TimeZone,Calendar.get()
GregorianCalendar是一個具體子類(它具有從接口或抽象類繼承的所有成員的實現)日曆它實現了我們熟悉的最廣泛使用的公曆。
java.util.GregorianCalendar 與 java.util.Calendar
GregorianCalendar 和 Calendar 類之間的主要區別在於,Calendar 類是抽象類,無法實例化。因此 Calendar 類的對象被初始化為:
Calendar cal = Calendar.getInstance();
這裏,Calendar 類的名為 cal 的對象使用默認區域設置和時區中的當前日期和時間進行初始化。而 GregorianCalendar 類是一個具體類,可以實例化。因此 GregorianCalendar 類的對象初始化為:
GregorianCalendar gcal = new GregorianCalendar();
這裏,一個名為 gcal 的對象GregorianCalendar類使用默認區域設置和時區中的當前日期和時間進行初始化。
字段定義:
GregorianCalendar Class defines two fields: AD : referring to the common era(anno Domini) BC : referring to before common era(Before Christ)
構造函數:有幾個構造函數GregorianCalendar對象。大致分類,構造函數為GregorianCalendar或者用初始化對象用戶在默認區域設置和時區中指定的日期和/或時間,或者用以下方法初始化對象用戶指定的區域設置和/或時區的默認日期和時間。具體如下:
構造函數簽名 | 說明 |
---|---|
GregorianCalendar() | 使用默認區域設置和時區中的當前日期和時間初始化對象 |
GregorianCalendar(int year, int month, int dayOfMonth) | 使用默認語言環境和時區中作為參數傳遞的 date-set 初始化對象 |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) | 使用默認區域設置和時區中作為參數傳遞的日期和 time-set 初始化對象 |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) | 使用默認區域設置和時區中作為參數傳遞的日期和更具體的 time-set 來初始化對象 |
GregorianCalendar(Locale locale) | 使用默認時區中的當前日期和時間以及作為參數傳遞的區域設置來初始化對象 |
GregorianCalendar(TimeZone timeZone) | 使用默認區域設置中的當前日期和時間以及作為參數傳遞的時區來初始化對象 |
GregorianCalendar(TimeZone timeZone, Locale locale) | 使用區域設置中的當前日期和時間以及作為參數傳遞的時區來初始化對象 |
方法from(),toZonedDateTime(),getCalendarType()在 JDK 8 中引入。
Java
// Java Program to show that Calendar class with
// default instantiation and GregorianCalendar class
// with default constructor is basically the same as both
// return the Gregorian Calendar for the default
// date, time, time zone and locale
import java.util.Calendar;
import java.util.GregorianCalendar;
class CalendarGFG {
public static void main(String[] args)
{
// Creating an object of Calendar Class
Calendar cal = Calendar.getInstance();
/* Creating an object of
GregorianCalendar Class */
GregorianCalendar gcal = new GregorianCalendar();
/* Displaying Current Date using
Calendar Class */
System.out.println("Calendar date: "
+ cal.getTime());
/* Displaying Current Date using
GregorianCalendar Class */
System.out.print("Gregorian date: "
+ gcal.getTime());
} // end of main function
} // end of class
輸出:
Calendar date: Sat Apr 28 13:36:37 UTC 2018 Gregorian date: Sat Apr 28 13:36:37 UTC 2018
演示各種構造函數的用法的示例:
1.使用默認構造函數
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of GregorianCalendar class
using default constructor*/
GregorianCalendar gcal = new GregorianCalendar();
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
} // end of main function
} // end of class
輸出:
Date: Apr 30, 2018 Time: 10:21:51 PM Time Zone: Coordinated Universal Time Locale: English (United States)
2. 通過傳遞年、月、日作為參數:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of GregorianCalendar class
by specifying year, month and dayOfMonth */
GregorianCalendar gcal = new GregorianCalendar(2018, 3, 30);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
} // end of main function
} // end of class
輸出:
Date: Apr 30, 2018 Time: 0:0:0 AM Time Zone: Coordinated Universal Time Locale: English (United States)
3. 通過傳遞年、月、日、小時、分鍾:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of GregorianCalendar class
by specifying year, month, dayOfMonth,
hourOfDay and minute */
GregorianCalendar gcal = new GregorianCalendar(2018, 3, 30, 10, 21);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
} // end of main function
} // end of class
輸出:
Date: Apr 30, 2018 Time: 10:21:0 AM Time Zone: Coordinated Universal Time Locale: English (United States)
4. 通過傳遞年、月、日、時、分、秒:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of GregorianCalendar class
by specifying year, month, dayOfMonth,
hourOfDay, minute and second */
GregorianCalendar gcal = new GregorianCalendar(2018, 3, 30, 10, 21, 51);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
} // end of main function
} // end of class
輸出:
Date: Apr 30, 2018 Time: 10:21:51 AM Time Zone: Coordinated Universal Time Locale: English (United States)
5. 通過傳遞 timeZone 作為參數:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of TimeZone class to create
an object of GregorianCalendar class to assign
an user defined time zone (GMT + 5:30)*/
TimeZone tz = TimeZone.getTimeZone("GMT+5:30");
GregorianCalendar gcal = new GregorianCalendar(tz);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + Locale.getDefault().getDisplayCountry());
} // end of main function
} // end of class
輸出:
Date: May 1, 2018 Time: 4:24:7 AM Time Zone: GMT+05:30 Locale: United States
6. 通過將區域設置作為參數傳遞:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of Locale class to create
an object of GregorianCalendar class to assign
an user defined locale (India)*/
Locale l = new Locale("en", "IN");
GregorianCalendar gcal = new GregorianCalendar(l);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: "
+ gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + l.getDisplayCountry());
} // end of main function
} // end of class
輸出:
Date: Apr 30, 2018 Time: 10:58:30 PM Time Zone: Coordinated Universal Time Locale: India
7. 通過傳遞 timeZone 和 locale 作為參數:
Java
// Java program to demonstrate simple GregorianCalendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
// declaring an array to store month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
// declaring an array to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an object of TimeZone class and Locale
class to create an object of GregorianCalendar
class to assign an user defined time zone
(GMT + 5:30) and locale (India)*/
TimeZone tz = TimeZone.getTimeZone("GMT+5:30");
Locale l = new Locale("en", "IN");
GregorianCalendar gcal = new GregorianCalendar(tz, l);
// displaying the date, time, time zone and locale
System.out.print("Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: "
+ gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + l.getDisplayCountry());
} // end of main function
} // end of class
輸出:
Date: May 1, 2018 Time: 4:34:59 AM Time Zone: GMT+05:30 Locale: India
參考:
GregorianCalendar(Java 平台 SE 8) - Oracle 幫助中心
相關用法
- Java Java.util.GregorianCalendar.add()用法及代碼示例
- Java Java.util.GregorianCalendar.clone()用法及代碼示例
- Java Java.util.GregorianCalendar.computeFields()用法及代碼示例
- Java Java.util.GregorianCalendar.computeTime()用法及代碼示例
- Java Java.util.GregorianCalendar.equals()用法及代碼示例
- Java Java.util.GregorianCalendar.getMaximum()用法及代碼示例
- Java Java.util.GregorianCalendar.getMinimum()用法及代碼示例
- Java Java.util.GregorianCalendar.getTimeZone()用法及代碼示例
- Java Java.util.GregorianCalendar.hashCode()用法及代碼示例
- Java Java.util.GregorianCalendar.isLeapYear()用法及代碼示例
- Java Java.util.GregorianCalendar.roll()用法及代碼示例
- Java Java.util.GregorianCalendar.setTimeZone()用法及代碼示例
- Java Java.util.ArrayDeque.add()用法及代碼示例
- Java Java.util.ArrayDeque.addFirst()用法及代碼示例
- Java Java.util.ArrayDeque.addLast()用法及代碼示例
- Java Java.util.ArrayDeque.clear()用法及代碼示例
- Java Java.util.ArrayDeque.clone()用法及代碼示例
- Java Java.util.ArrayDeque.descendingIterator()用法及代碼示例
- Java Java.util.ArrayDeque.element()用法及代碼示例
- Java Java.util.ArrayDeque.getFirst()用法及代碼示例
- Java Java.util.ArrayDeque.getLast()用法及代碼示例
- Java Java.util.ArrayDeque.isEmpty()用法及代碼示例
- Java Java.util.ArrayDeque.iterator()用法及代碼示例
- Java Java.util.ArrayDeque.peek()用法及代碼示例
- Java Java.util.ArrayDeque.peekFirst()用法及代碼示例
注:本文由純淨天空篩選整理自noobhere大神的英文原創作品 Java.util.GregorianCalendar Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。