要使用Java Calendar类获取年月日,可以按照以下步骤进行操作:
java.util.Calendar
类:import java.util.Calendar;
Calendar calendar = Calendar.getInstance();
get
方法获取年、月、日的值:int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1; // 注意,Calendar类中的月份是从0开始计数的,所以需要+1
int day = calendar.get(Calendar.DAY_OF_MONTH);
完整示例代码如下:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("年:" + year);
System.out.println("月:" + month);
System.out.println("日:" + day);
}
}
输出结果为当前日期的年、月、日。