Java 如何為當前時間和日期增加天數
在學習如何增加和減少日期之前,人們必須先熟悉Java日歷類。在本節中,我們將通過實例來討論 如何在Java中為當前時間和日期增加天數 。
Java日歷API的add()函數是獨一無二的,它為指定的單位增加給定的整數。
Java日歷類提供了在某個時間點和一些日歷字段之間轉換日期的方法,如MONTH、YEAR、HOUR等。它是的一個子類,支持、以及接口。
因為它是一個抽象類,所以我們無法使用函數() { [本地代碼] }來創建它的實例。我們必須使用靜態方法來設計和實現一個子類。 ()。
使用默認時區的當前時間和默認區域設置,.()返回一個日歷對象。
.( z)
.( aL)
.( z, aL)
在Java中遞減日期
請遵守下面的說明sql 一個日期加上天數,在Java中減少日期。
在Java日歷類中添加以下包。
import java.util.Calendar ;
首先,創建一個日歷對象sql 一個日期加上天數,然后在其上顯示當前日期。
Calendar c = Calendar.getInstance();
System.out.println(" The present date is : " +c.getTime());
讓我們使用add()方法以及來遞減現在的日期。DATE不變。由于日期正在減少,所以設置一個負數。
c.add(Calendar.DATE,-2);
文件名:.java
import java.util.Calendar;
public class Decrement {
public static void main(String args[]) {
Calendar c = Calendar.getInstance() ;
System.out.println(" The present Date is = " + c.getTime()) ;
// Decreasing the present date by 2
c.add(Calendar.DATE, -2) ;
System.out.println(" The date after decreasing is = " + c.getTime()) ;
}
}
輸出 。
The present Date is = Thu Jan 19 16:12:39 IST 2023
The date after decreasing is = Tue Jan 17 16:12:39 IST 2023
在Java中增加日期
請遵守下面的說明,在Java中增加日期。
在Java日歷類中添加以下包。
import java.util.Calendar ;
首先創建一個日歷對象,然后在上面顯示當前日期。
Calendar c = Calendar.getInstance();
System.out.println(" The present date is : " +c.getTime());
讓我們使用add()方法以及日歷來增加現在的日期。DATE不變。由于日期被增加,所以設置一個正數。
c.add(Calendar.DATE, 3);
文件名:.java
import java.util.Calendar;
public class Increment {
public static void main(String args[]) {
Calendar c = Calendar.getInstance() ;
System.out.println(" The present Date is = " + c.getTime()) ;
// increasing the present date by 2
c.add(Calendar.DATE, 2) ;
System.out.println(" The date after increasing is = " + c.getTime()) ;
}
}
輸出 。
The present Date is = Thu Jan 19 16:17:03 IST 2023
The date after increasing is = Sat Jan 21 16:17:03 IST 2023
一個同時顯示日期遞增和遞減的程序。
文件名:t.java
import java.util.Calendar;
public class IncrementAndDecrement {
public static void main(String args[]) {
Calendar c = Calendar.getInstance() ;
System.out.println(" The present Date is = " + c.getTime()) ;
// Decreasing the date by 3
c.add(Calendar.DATE, -3) ;
System.out.println(" The date before 3 days= " + c.getTime()) ;
// Increasing the date by 6
c.add(Calendar.DATE, 9) ;
System.out.println(" The date after 6 days = " + c.getTime()) ;
}
}
輸出 。
The present Date is = Thu Jan 19 16:22:40 IST 2023
The date before 3 days= Mon Jan 16 16:22:40 IST 2023
The date after 6 days = Wed Jan 25 16:22:40 IST 2023
請看下面一節中的示例,它將當前日期增加了3年2個月和1天。
文件名:.java
import java.text.SimpleDateFormat ;
import java.util.Calendar ;
import java.util.Date ;
public class AddingDays {
public static void main(String args[]) {
// creating the present date in java
Date d = new Date() ;
//outputting the date in user reading format.
SimpleDateFormat dF = new SimpleDateFormat("yyyy-MM-dd") ;
String dS = dF.format(d) ;
System.out.println(" The present date is : "+dS) ;
// creating the instance for calendar class
Calendar c = Calendar.getInstance();
c.setTime(d) ;
// adding 3 years, 2 months, and 1 day
c.add(Calendar.YEAR, 3) ;
c.add(Calendar.MONTH, 2) ;
c.add(Calendar.DATE, 1) ;
// getting the new date from the calendar
Date d1 = c.getTime() ;
// printing the new date
String newDate = dF.format(d1) ;
System.out.println(" The updated date is : "+newDate) ;
}
}
輸出 。
The present date is : 2023-01-19
The updated date is : 2026-03-20
讓我們來看看下面這個例子,它的時間跨度從現在開始往前推3年2個月零1天。
文件名:.java
import java.text.SimpleDateFormat ;
import java.time.LocalDateTime ;
import java.time.ZoneId ;
import java.time.format.DateTimeFormatter ;
import java.util.Date ;
public class DecreasingDays {
public static void main(String args[]) {
// The formats of the date
String f = "yyyy-MM-dd hh:mm:ss" ;
SimpleDateFormat dF = new SimpleDateFormat(f) ;
// The Date format in terms of java 8
DateTimeFormatter dF8 = DateTimeFormatter.ofPattern(f) ;
// creating the date in java
Date d = new Date() ;
//outputting the date in user reading format.
String dS = dF.format(d) ;
System.out.println(" The present date is : " + dS) ;
// manipulating the date to LocalDateTime
LocalDateTime local = d.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() ;
System.out.println(" localDateTime : " + dF8.format(local)) ;
local = local.minusYears(3) ;
local = local.minusMonths(2) ;
local = local.minusDays(1) ;
local = local.minusHours(3).minusMinutes(2).minusSeconds(1) ;
// manupulating the LocalDateTime to Date
Date d1 = Date.from(local.atZone(ZoneId.systemDefault()).toInstant()) ;
// outputting the new date
String newDate = dF.format(d1);
System.out.println(" The updated date after going back 3 years is : " + newDate);
}
}
輸出 。
The present date is : 2023-01-19 04:38:26
localDateTime : 2023-01-19 04:38:26
The updated date after going back 3 years is : 2019-11-18 01:36:25