當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Calendar Class用法及代碼示例


Java中的Calendar類是一個抽象類,它提供了在特定時間點和一組日曆字段(如MONTH、YEAR、HOUR等)之間轉換日期的方法。它繼承Object類並實現Comparable、Serializable、Cloneable接口。

由於它是一個抽象類,所以我們不能使用構造函數來創建實例。相反,我們必須使用靜態方法 Calendar.getInstance() 來實例化並實現sub-class。

  • Calendar.getInstance():返回基於默認時區、默認區域設置的當前時間的 Calendar 實例。
  • Calendar.getInstance(時區)
  • Calendar.getInstance(區域設置 aLocale)
  • Calendar.getInstance(TimeZone zone, Locale aLocale)

Java程序演示getInstance()方法


// Date getTime(): It is used to return a 
// Date object representing this 
// Calendar's time value. 
  
import java.util.*; 
public class Calendar1 { 
    public static void main(String args[]) 
    { 
        Calendar c = Calendar.getInstance(); 
        System.out.println("The Current Date is:" + c.getTime()); 
    } 
} 
輸出:
The Current Date is:Tue Aug 28 11:10:40 UTC 2018

Important Methods and their usage







METHOD DESCRIPTION
抽象無效添加(int字段,int數量) 它用於根據日曆的規則向給定日曆字段添加或減去指定的時間量。
int get(int 字段) 它用於返回給定日曆字段的值。
摘要 int getMaximum(int 字段) 它用於返回此 Calendar 實例的給定日曆字段的最大值。
摘要 int getMinimum(int 字段) 它用於返回此 Calendar 實例的給定日曆字段的最小值。
日期getTime() 它用於返回表示此日曆的時間值的 Date 對象。</td

以下示例程序旨在說明上述方法:

程序1::Java程序演示get()方法。


// Program to demonstrate get() method 
// of Calendar class 
  
import java.util.*; 
public class Calendar2 { 
    public static void main(String[] args) 
    { 
        // creating Calendar object 
        Calendar calendar = Calendar.getInstance(); 
          
        // Demonstrate Calendar's get()method 
        System.out.println("Current Calendar's Year: " + calendar.get(Calendar.YEAR)); 
        System.out.println("Current Calendar's Day: " + calendar.get(Calendar.DATE)); 
        System.out.println("Current MINUTE: " + calendar.get(Calendar.MINUTE)); 
        System.out.println("Current SECOND: " + calendar.get(Calendar.SECOND)); 
    } 
} 
輸出:
Current Calendar's Year: 2018
Current Calendar's Day: 28
Current MINUTE: 10
Current SECOND: 45

程序2::Java程序演示getMaximum()方法。


// Program to demonstrate getMaximum() method 
// of Calendar class 
  
import java.util.*; 
public class Calendar3 { 
    public static void main(String[] args) 
    { 
        // creating calendar object 
        Calendar calendar = Calendar.getInstance(); 
      
        int max = calendar.getMaximum(Calendar.DAY_OF_WEEK); 
        System.out.println("Maximum number of days in a week: " + max); 
          
        max = calendar.getMaximum(Calendar.WEEK_OF_YEAR); 
        System.out.println("Maximum number of weeks in a year: " + max); 
    } 
} 
輸出:
Maximum number of days in a week: 7
Maximum number of weeks in a year: 53

程序3::Java程序演示getMinimum()方法。


// Program to demonstrate getMinimum() method 
// of Calendar class 
  
import java.util.*; 
public class Calendar4 { 
    public static void main(String[] args) 
    { 
        // creating calendar object 
        Calendar calendar = Calendar.getInstance(); 
          
        int min = calendar.getMinimum(Calendar.DAY_OF_WEEK); 
        System.out.println("Minimum number of days in week: " + min); 
          
        min = calendar.getMinimum(Calendar.WEEK_OF_YEAR); 
        System.out.println("Minimum number of weeks in year: " + min); 
    } 
} 
輸出:
Minimum number of days in week: 1
Minimum number of weeks in year: 1

程序4::Java程序演示add()方法。


// Program to demonstrate add() method 
// of Calendar class 
  
import java.util.*; 
public class Calendar5 { 
    public static void main(String[] args) 
    { 
        // creating calendar object 
        Calendar calendar = Calendar.getInstance(); 
        calendar.add(Calendar.DATE, -15); 
        System.out.println("15 days ago: " + calendar.getTime()); 
        calendar.add(Calendar.MONTH, 4); 
        System.out.println("4 months later: " + calendar.getTime()); 
        calendar.add(Calendar.YEAR, 2); 
        System.out.println("2 years later: " + calendar.getTime()); 
    } 
} 
輸出:
15 days ago: Mon Aug 13 11:10:57 UTC 2018
4 months later: Thu Dec 13 11:10:57 UTC 2018
2 years later: Sun Dec 13 11:10:57 UTC 2020

參考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html



相關用法


注:本文由純淨天空篩選整理自Poojitha Reddy 1大神的英文原創作品 Calendar Class in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。