Java程序,从GregorianCalender显示前一天
对于GregorianCalendar类,请导入以下包。
import java.util.GregorianCalendar;
创建一个对象。
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
现在,使用以下字段并使用负数(-1)的add()方法显示前一天。
cal.add((GregorianCalendar.DATE), -1)
示例
import java.util.Calendar; import java.util.GregorianCalendar; public class Demo { public static void main(String[] a) { GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance(); System.out.println("Current date: " + cal.getTime()); // past date cal.add((GregorianCalendar.DATE), -1); System.out.println("Modified date (Previous Day): " + cal.getTime()); } }
输出结果
Current date: Mon Nov 19 18:12:53 UTC 2018 Modified date (Previous Day): Sun Nov 18 18:12:53 UTC 2018