package org.example.calendar;
?
import java.util.Calendar;
?
public class CalendarDemo {
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.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
//一年后的今天
calendar.add(Calendar.YEAR,1);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
//一个星期前的今天
calendar.add(Calendar.DATE,-7);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
// 设置当前日历的年月日
calendar.set(2021,9,1);
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH)+1;
day = calendar.get(Calendar.DATE);
System.out.println(year+"年"+month+"月"+day+"日");
}
}
package org.example.calendar;
?
import java.util.Calendar;
import java.util.Scanner;
?
public class FebruaryDay {
/**
* 获取任意年份的二月天
* @param args
*/
public static void main(String[] args) {
// 键盘录入任意年份
Scanner scanner=new Scanner(System.in);
System.out.println("请输入年份: ");
int year=scanner.nextInt();
//设置日历对象的年月日(设置为3月1日)
Calendar calendar=Calendar.getInstance();
calendar.set(year,2,1);//月份从0开始,所以month设置为2
//3月1日的前一天就是二月的最后一天
calendar.add(Calendar.DATE,-1);
int date = calendar.get(Calendar.DATE);
System.out.println(year+"年的二月有: "+date+"天");
}
}
原文:https://www.cnblogs.com/yts-helloworld/p/15354827.html