Java Calendar类( java.util.Calendar )是Java日期时间操作中非常有用且方便的类。 在这里,我将演示如何用日历类修改日期时间。
使用Calendar()获取当前日期时间
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
日历日期时间操作功能
//Add one day to current date time
cal.add(Calendar.DATE, 1);
//Add one month to current date time
cal.add(Calendar.MONTH, 1);
//Add one year to current date time
cal.add(Calendar.YEAR, 1);
//Add one hour to current date time
cal.add(Calendar.HOUR, 1);
//Add one minute to current date time
cal.add(Calendar.MINUTE, 1);
//Add one second to current date time
cal.add(Calendar.SECOND, 1);
//Subtract one day from current date
cal.add(Calendar.DATE, -1);
//Subtract one month from current date
cal.add(Calendar.MONTH, -1);
//Subtract one year from current date
cal.add(Calendar.YEAR, -1);
//Subtract one hour from current date
cal.add(Calendar.HOUR, -1);
//Subtract one minute from current date
cal.add(Calendar.MINUTE, -1);
//Subtract one second from current date
cal.add(Calendar.SECOND, -1);
这是完整的源代码,显示了如何在Java中修改日期时间
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class DateTimeManipulation {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
cal.add(Calendar.DATE, 1);
System.out.println("Add one day to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);
System.out.println("Add one month to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);
System.out.println("Add one year to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
System.out.println("Add one hour to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 1);
System.out.println("Add one minute to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 1);
System.out.println("Add one second to current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println("Subtract one day from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
System.out.println("Subtract one month from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
System.out.println("Subtract one year from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
System.out.println("Subtract one hour from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -1);
System.out.println("Subtract one minute from current date : " + dateFormat.format(cal.getTime()));
cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -1);
System.out.println("Subtract one second from current date : " + dateFormat.format(cal.getTime()));
}
}
输出量
当前日期时间:2008/12/28 10:24:53
将当前日期添加一天:2008/12/29 10:24:53
将当前日期添加一个月:2009/01/28 10:24:53
将当前日期添加一年:2009/12/28 10:24:53
当前日期增加一小时:2008/12/28 11:24:53
添加一分钟到当前日期:2008/12/28 10:25:53
向当前日期添加一秒:2008/12/28 10:24:54
从当前日期减去一天:2008/12/27 10:24:53
从当前日期减去一个月:2008/11/28 10:24:53
从当前日期减去一年:2007/12/28 10:24:53
从当前日期减去一小时:2008/12/28 09:24:53
从当前日期减去一分钟:2008/12/28 10:23:53
从当前日期减去一秒:2008/12/28 10:24:52
翻译自: https://mkyong.com/java/how-to-modify-date-time-date-manipulation-java/