Java中的日历类和示例
Java中的Calendar类是一个抽象类,它提供了在特定时刻和一组日历字段(如MONTH、YEAR、HOUR等)之间转换日期的方法。它继承了Object类并实现了Comparable、Serializable、Cloneable接口。
由于它是一个抽象类,所以我们不能使用构造函数来创建实例。相反,我们将不得不使用静态方法 Calendar.getInstance() 来实例化和实现一个子类。
- Calendar.getInstance():根据默认时区的当前时间,使用默认语言环境返回一个 Calendar 实例。
- Calendar.getInstance(TimeZone 区域)
- Calendar.getInstance(Locale aLocale)
- Calendar.getInstance(TimeZone zone, Locale aLocale)
演示 getInstance() 方法的Java程序:
// 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
重要方法及其用法
METHOD | DESCRIPTION |
---|---|
abstract void add(int field, int amount) | It is used to add or subtract the specified amount of time to the given calendar field, based on the calendar’s rules. |
int get(int field) | It is used to return the value of the given calendar field. |
abstract int getMaximum(int field) | It is used to return the maximum value for the given calendar field of this Calendar instance. |
abstract int getMinimum(int field) | It is used to return the minimum value for the given calendar field of this Calendar instance. |
Date getTime() | It is used to return a Date object representing this Calendar’s time value. |
下面的程序说明了上述方法:
程序 1 :演示 get() 方法的Java程序。
// 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 :演示 getMaximum() 方法的Java程序。
// 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 :演示 getMinimum() 方法的Java程序。
// 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 :演示 add() 方法的Java程序。
// 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: Java/util/Calendar.html