Calendar類中的set(int calndr_field,int new_val)方法用於將calndr_field值設置為new_val。此日曆的舊字段將替換為新字段。
用法:
public void set(int calndr_field, int new_val)
參數:該方法有兩個參數:
- calndr_field:這是“日曆”類型,是指要更改的日曆字段。
- new_val:這是指要替換的新值。
返回值:該方法不返回任何值。
以下示例程序旨在說明Calendar類的set()方法的用法:
示例1:
// Java code to illustrate
// set() method
import java.util.*;
public class Calendar_Demo {
public static void main(String args[])
{
// Creating a calendar
Calendar calndr = Calendar.getInstance();
// Displaying the month
System.out.println("The Current Month is: "
+ calndr.get(
Calendar.MONTH));
// Replacing with a new value
calndr.set(Calendar.MONTH, 11);
// Displaying the modified result
System.out.println("Altered Month is: "
+ calndr.get(
Calendar.MONTH));
}
}
輸出:
The Current Month is: 1 Altered Month is: 11
示例2:
// Java code to illustrate
// set() method
import java.util.*;
public class Calendar_Demo {
public static void main(String args[])
{
// Creating a calendar
Calendar calndr = Calendar.getInstance();
// Displaying the Year
System.out.println("The Current year is: "
+ calndr.get(
Calendar.YEAR));
// Replacing with a new value
calndr.set(Calendar.YEAR, 1996);
// Displaying the modified result
System.out.println("Altered year is: "
+ calndr.get(
Calendar.YEAR));
}
}
輸出:
The Current year is: 2019 Altered year is: 1996
參考: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#set-int-int-
相關用法
- Java Calendar get()用法及代碼示例
- Java Calendar add()用法及代碼示例
- Java Calendar internalGet()用法及代碼示例
- Java Calendar isSet()用法及代碼示例
- Java Calendar getTimeZone()用法及代碼示例
- Java Calendar getInstance()用法及代碼示例
- Java Calendar getGreatestMinimum()用法及代碼示例
- Java Calendar getAvailableLocales()用法及代碼示例
- Java Calendar getDisplayName()用法及代碼示例
- Java Calendar getDisplayNames()用法及代碼示例
- Java Calendar getFirstDayOfWeek()用法及代碼示例
- Java Calendar getLeastMaximum()用法及代碼示例
- Java Calendar hashCode()用法及代碼示例
- Java Calendar getTime()用法及代碼示例
- Java Calendar roll()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Calendar set() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。