Calendar的简单使用
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+"天");
}
}
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!