导入以下程序包以使用Calendar类,
import java.util.Calendar;
创建一个日历对象,如下所示-
Calendar cal = Calendar.getInstance();
现在让我们获取当前日期,年份和月份。
cal.get(Calendar.YEAR) cal.get(Calendar.MONTH) + 1 cal.get(Calendar.DATE)
以下是最后一个示例。
import java.util.Calendar; public class Demo { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Year = " + cal.get(Calendar.YEAR)); System.out.println("Month = " + (cal.get(Calendar.MONTH) + 1)); System.out.println("Date = " + cal.get(Calendar.DATE)); } }
输出结果
Year = 2018 Month = 11 Date = 19