Java 如何使用日历类来改变日期和时间
Java日历类(java.util.Calendar)是一个非常有用和实用的类,用于在Java中操作日期和时间。在这里,我将向你展示如何使用日历类来改变日期和时间。
使用calendar()类方法获得今天的日期和时间。
DateFormat dF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar c = Calendar.getInstance();
System.out.println(" Present Date Time is : " + dF.format(c.getTime()));
改变日历上的日期或时间。
// Date and time are current plus one day.
cal.add(Calendar.DATE, 1);
// Date and time are current plus one month.
c.add(Calendar.MONTH, 1);
// Date and time are current plus one year.
c.add(Calendar.YEAR, 1);
// Date and time are current plus one hour.
c.add(Calendar.HOUR, 1);
// Date and time are current plus one minute.
c.add(Calendar.MINUTE, 1);
// Date and time are current plus one second.
c.add(Calendar.SECOND, 1);
// Take a day out of the current date.
c.add(Calendar.DATE, -1);
// Take a month out of the current date.
c.add(Calendar.MONTH, -1);
// Take a year out of the current date.
c.add(Calendar.YEAR, -1);
// Take an hour out of the current date.
c.add(Calendar.HOUR, -1);
// Take a minute out of the current date.
c.add(Calendar.MINUTE, -1);
// Take a second out of the current date.
c.add(Calendar.SECOND, -1);
这个完整的源代码演示了如何在Java中改变日期和时间。
// 文件名:DateTimeChanging.java
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTimeChanging {
public static void main(String args[]) {
DateFormat dF = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// retrieve the current time and date with a calendar ()
Calendar c = Calendar.getInstance();
System.out.println(" Present Date Time is : " + dF.format(c.getTime()));
// Date and time are current plus one day.
c.add(Calendar.DATE, 1);
System.out.println(" Adding one day to the present date : " + dF.format(c.getTime()));
// Date and time are current plus one month.
c = Calendar.getInstance();
c.add(Calendar.MONTH, 1);
System.out.println(" Adding one month to the present date : " + dF.format(c.getTime()));
// Date and time are current plus one year.
c = Calendar.getInstance();
c.add(Calendar.YEAR, 1);
System.out.println(" Adding one year to the present date : " + dF.format(c.getTime()));
// Date and time are current plus one hour.
c = Calendar.getInstance();
c.add(Calendar.HOUR, 1);
System.out.println(" Adding one hour to the present date : " + dF.format(c.getTime()));
// Date and time are current plus one minute.
c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
System.out.println(" Adding one minute to the present date : " + dF.format(c.getTime()));
// Date and time are current plus one second.
c = Calendar.getInstance();
c.add(Calendar.SECOND, 1);
System.out.println(" Adding one second to the present date : " + dF.format(c.getTime()));
// Take a day out of the current date.
c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
System.out.println(" Subtracting one day from the present date : " + dF.format(c.getTime()));
// Take a day out of the current month.
c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
System.out.println(" Subtracting one month from the present date : " + dF.format(c.getTime()));
// Take a day out of the current year.
c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
System.out.println(" Subtracting one year from the present date : " + dF.format(c.getTime()));
// Take a day out of the current hour.
c = Calendar.getInstance();
c.add(Calendar.HOUR, -1);
System.out.println(" Subtracting one hour from the present date : " + dF.format(c.getTime()));
// Take a day out of the current minute.
c = Calendar.getInstance();
c.add(Calendar.MINUTE, -1);
System.out.println(" Subtracting one minute from the present date : " + dF.format(c.getTime()));
// Take a day out of the current second.
c = Calendar.getInstance();
c.add(Calendar.SECOND, -1);
System.out.println(" Subtracting one second from the present date : " + dF.format(c.getTime()));
}
}
输出 。
Present Date Time is : 2023/01/14 18:15:26
Adding one day to the present date : 2023/01/15 18:15:26
Adding one month to the present date : 2023/02/14 18:15:26
Adding one year to the present date : 2024/01/14 18:15:26
Adding one hour to the present date : 2023/01/14 19:15:26
Adding one minute to the present date : 2023/01/14 18:16:26
Adding one second to the present date : 2023/01/14 18:15:27
Subtracting one day from the present date : 2023/01/13 18:15:26
Subtracting one month from the present date : 2022/12/14 18:15:26
Subtracting one year from the present date : 2022/01/14 18:15:26
Subtracting one hour from present date : 2023/01/14 17:15:26
Subtracting one minute from the present date : 2023/01/14 18:14:26
Subtracting one second from the present date : 2023/01/14 18:15:25