Calendar類中的setTime(Date dt)方法用於設置此Calendar的時間值表示的Calendars時間,並以給定或通過的日期為參數。
用法:
public final void setTime(Date dt))
參數:該方法采用Date類型的一個參數dt表示要設置的給定日期。
返回值:該方法不返回任何值。
以下示例程序旨在說明Calendar類的setTime()方法的用法:
示例1:
// Java code to illustrate
// setTime() method
import java.util.*;
public class Calendar_Demo
extends GregorianCalendar {
public static void main(String[] args)
{
// Creating calendar objects
Calendar calndr1
= (Calendar)Calendar.getInstance();
Calendar calndr2
= (Calendar)Calendar.getInstance();
// Displaying the current date
System.out.println("The Current"
+ " System Date: "
+ calndr1.getTime());
// Setting to a different date
calndr1.set(Calendar.MONTH, 5);
calndr1.set(Calendar.YEAR, 2006);
calndr1.set(Calendar.DAY_OF_WEEK, 15);
Date dt = calndr1.getTime();
// Setting the timevalue
calndr2.setTime(dt);
// Displaying the new date
System.out.println("The modified"
+ " Date:"
+ calndr2.getTime());
}
}
輸出:
The Current System Date: Fri Feb 22 07:33:13 UTC 2019 The modified Date:Sun Jun 18 07:33:13 UTC 2006
示例2:
// Java code to illustrate
// setTime() method
import java.util.*;
public class Calendar_Demo
extends GregorianCalendar {
public static void main(String[] args)
{
// Creating calendar objects
Calendar calndr1
= (Calendar)Calendar.getInstance();
Calendar calndr2
= (Calendar)Calendar.getInstance();
// Displaying the current date
System.out.println("The Current"
+ " System Date: "
+ calndr1.getTime());
// Setting to a different date
calndr1.set(Calendar.MONTH, 10);
calndr1.set(Calendar.YEAR, 1995);
calndr1.set(Calendar.DAY_OF_WEEK, 20);
Date dt = calndr1.getTime();
// Setting the timevalue
calndr2.setTime(dt);
// Displaying the new date
System.out.println("The modified"
+ " Date: "
+ calndr2.getTime());
}
}
輸出:
The Current System Date: Fri Feb 22 07:33:20 UTC 2019 The modified Date: Fri Nov 24 07:33:20 UTC 1995
參考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date)
相關用法
- Java Date setTime()用法及代碼示例
- Java Calendar get()用法及代碼示例
- Java Calendar set()用法及代碼示例
- Java Calendar add()用法及代碼示例
- Java SQL Timestamp setTime()用法及代碼示例
- Java Calendar setFirstDayOfWeek()用法及代碼示例
- Java Calendar internalGet()用法及代碼示例
- Java Calendar clone()用法及代碼示例
- Java Calendar setLenient()用法及代碼示例
- Java Calendar getMinimum()用法及代碼示例
- Java Calendar getMinimalDaysInFirstWeek()用法及代碼示例
- Java Calendar clear()用法及代碼示例
- Java Calendar setTimeZone()用法及代碼示例
- Java Calendar getLeastMaximum()用法及代碼示例
- Java Calendar getMaximum()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Calendar setTime() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。