时间和日期
需要知道的前提:
过去,标准时间时把地球自转一周的时间长度划分为24个小时,太阳直射某一经度的时间认定为12:00,这个某一经度指的是本初子午线,就是横穿格林尼治天文台那条线,这就是过去的世界时,GreenwichMeanTime,简称GMT。但是呢,地球自转并非匀速,存在误差;
为了更精确表达时间,利用原子钟来计算时间,利用铯原子的震动次数来表达时段,(当然也有其他原子),据此确定的世界时间成为协调时间时,即UTC,这种计算方式被广泛应用于计算机领域。
此外,需要记住,中国位于东八区,即UTC+8;
时间的进制就不说了,之前有提到过;
JDK7之前
Date类
对象的创建:
// 创建日期对象,把当前的时间
System.out.println(new Date()); // Sat Mar 16 17:43:32 CST 2024
// 创建日期对象,把当前的毫秒值转成日期对象
System.out.println(new Date(0L)); // Thu Jan 01 08:00:00 CST 1970
常用方法:
public class DateDemo02 {
public static void main(String[] args) {
//创建日期对象
Date d = new Date();
//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
System.out.println(d.getTime());
System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
//public void setTime(long time):设置时间,给的是毫秒值
long time = 1000*60*60;
long time = System.currentTimeMillis();
d.setTime(time);
System.out.println(d);
}
}
SimpleDateFormat
这个类是日期/时间格式化类,我们通过这个类可以帮我们完成日期和文本之间的转换,也就是可以在Date对象与String对象之间进行来回转换。可以通过下面这个表格来指示格式:
y | Year | Year |
---|---|---|
M | Month in year (context sensitive) | Month |
d | Day in month | Number |
a | Am/pm marker | Text |
H | Hour in day (0-23) | Number |
m | Minute in hour | Number |
s | Second in minute | Number |
常用方法:
public static void main(String[] args) throws ParseException {
/*
public simpleDateFormat() 默认格式
public simpleDateFormat(String pattern) 指定格式
public final string format(Date date) 格式化(日期对象 ->字符串)
public Date parse(string source) 解析(字符串 ->日期对象)
*/
method1();
//1.定义一个字符串表示时间
String str = "2023-11-11 11:11:11";
//2.利用空参构造创建simpleDateFormat对象
// 细节:
//创建对象的格式要跟字符串的格式完全一致
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(str);
//3.打印结果
System.out.println(date.getTime());//1699672271000
}
private static void method1() {
//1.利用空参构造创建simpleDateFormat对象,默认格式
SimpleDateFormat sdf1 = new SimpleDateFormat();
Date d1 = new Date(0L);
String str1 = sdf1.format(d1);
System.out.println(str1);//1970/1/1 上午8:00
//2.利用带参构造创建simpleDateFormat对象,指定格式
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
String str2 = sdf2.format(d1);
System.out.println(str2);//1970年01月01日 08:00:00
}
练习一:
/*
出生年月日为:2000-11-11
请用字符串表示这个数据,并将其转换为:2000年11月11日
创建一个Date对象表示2000年11月11日
创建一个SimpleDateFormat对象,并定义格式为年月日把时间变成:2000年11月11日
*/
//1.可以通过2000-11-11进行解析,解析成一个Date对象
String str = "2000-11-11";
//2.解析
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf1.parse(str);
//3.格式化
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
String result = sdf2.format(date);
System.out.println(result);
练习二:
public static void main(String[] args) throws ParseException {
/* 需求:
秒杀活动开始时间:2023年11月11日 0:0:0(毫秒值)
秒杀活动结束时间:2023年11月11日 0:10:0(毫秒值)
小贾下单并付款的时间为:2023年11月11日 0:01:0
小皮下单并付款的时间为:2023年11月11日 0:11:0
用代码说明这两位同学有没有参加上秒杀活动?
*/
participateIn("2023-11-11 00:01:00");
participateIn("2023-11-11 00:11:00");
}
public static void participateIn(String time) throws ParseException {
String time1 = "2023-11-11 00:00:00";
String time2 = "2023-11-11 00:10:00";
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = simpleDateFormat.parse(time1);
Date date2 = simpleDateFormat.parse(time2);
Date date3 = simpleDateFormat.parse(time);
if (date3.getTime() > date1.getTime() && date3.getTime() < date2.getTime()) {
System.out.println("在这个时间段。");
} else {
System.out.println("不在这个时间段。");
}
}
Calendar
Calendar类表示一个“日历类”,可以进行日期运算。它是一个抽象类,不能创建对象,我们可以使用它的子类:java.util.GregorianCalendar类。有两种方式可以获取GregorianCalendar对象:直接创建GregorianCalendar对象;或者通过Calendar的静态方法getInstance()方法获取GregorianCalendar对象
get方法:
public static void main(String[] args) {
//创建日历对象
Calendar c = Calendar.getInstance();
// System.out.println(c);
//修改日历对象
Date d =new Date(0L);
c.setTime(d);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
int week = c.get(Calendar.DAY_OF_WEEK);//返回值范围:1--7,分别表示:"星期日","星期一","星期二",...,"星期六"
System.out.println(year + "年" + month + "月" + day + "日" +
hour + ":" + minute + ":" + second);
System.out.println(getWeek(week));
}
public static String getWeek(int w) {//w = 1 --- 7
//做一个表(数组)
String[] weekArray = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
// 索引 [0] [1] [2] [3] [4] [5] [6]
//查表
return weekArray[w - 1];
}
set方法:
public static void main(String[] args) {
//设置属性——set(int field,int value):
Calendar c1 = Calendar.getInstance();//获取当前日期
//计算班长出生那天是星期几(假如班长出生日期为:1998年3月18日)
c1.set(Calendar.YEAR, 1998);
c1.set(Calendar.MONTH, 3 - 1);//转换为Calendar内部的月份值
c1.set(Calendar.DAY_OF_MONTH, 18);
int w = c1.get(Calendar.DAY_OF_WEEK);
System.out.println("班长出生那天是:" + getWeek(w));
}
//查表法,查询星期几
public static String getWeek(int w) {//w = 1 --- 7
//做一个表(数组)
String[] weekArray = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
// 索引 [0] [1] [2] [3] [4] [5] [6]
//查表
return weekArray[w - 1];
}
}
JDK8及以后
JDK8之后主要是做了一些优化,代码写起来更方便,不必再转成毫秒比来比去;此外,安全性也得到了优化和提升;
常用方法:
JDK8时间类类名 | 作用 |
---|---|
ZoneId | 时区 |
Instant | 时间戳 |
ZoneDateTime | 带时区的时间 |
DateTimeFormatter | 用于时间的格式化和解析 |
LocalDate | 年、月、日 |
LocalTime | 时、分、秒 |
LocalDateTime | 年、月、日、时、分、秒 |
Duration | 时间间隔(秒,纳,秒) |
Period | 时间间隔(年,月,日) |
ChronoUnit | 时间间隔(所有单位) |
ZoneID代码演示:
/*
static Set<string> getAvailableZoneIds() 获取Java中支持的所有时区
static ZoneId systemDefault() 获取系统默认时区
static Zoneld of(string zoneld) 获取一个指定时区
*/
//1.获取所有的时区名称
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds.size());//603
System.out.println(zoneIds);// Asia/Shanghai
//2.获取当前系统的默认时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);//Asia/Shanghai
//3.获取指定的时区
ZoneId zoneId1 = ZoneId.of("Asia/Pontianak");
System.out.println(zoneId1);//Asia/Pontianak
}
Instant时间戳:
/*
static Instant now() 获取当前时间的Instant对象(标准时间)
static Instant ofXxxx(long epochMilli) 根据(秒/毫秒/纳秒)获取Instant对象
ZonedDateTime atZone(ZoneIdzone) 指定时区
boolean isxxx(Instant otherInstant) 判断系列的方法
Instant minusXxx(long millisToSubtract) 减少时间系列的方法
Instant plusXxx(long millisToSubtract) 增加时间系列的方法
*/
//1.获取当前时间的Instant对象(标准时间)
Instant now = Instant.now();
System.out.println(now);
//2.根据(秒/毫秒/纳秒)获取Instant对象
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);//1970-01-01T00:00:00z
Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);//1970-01-01T00:00:01Z
Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(instant3);//1970-01-01T00:00:027
//3. 指定时区
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);
//4.isXxx 判断
Instant instant4=Instant.ofEpochMilli(0L);
Instant instant5 =Instant.ofEpochMilli(1000L);
//5.用于时间的判断
//isBefore:判断调用者代表的时间是否在参数表示时间的前面
boolean result1=instant4.isBefore(instant5);
System.out.println(result1);//true
//isAfter:判断调用者代表的时间是否在参数表示时间的后面
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);//false
//6.Instant minusXxx(long millisToSubtract) 减少时间系列的方法
Instant instant6 =Instant.ofEpochMilli(3000L);
System.out.println(instant6);//1970-01-01T00:00:03Z
Instant instant7 =instant6.minusSeconds(1);
System.out.println(instant7);//1970-01-01T00:00:02Z
ZoneDateTime 带时区时间
//1.获取当前时间对象(带时区)
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
//2.获取指定的时间对象(带时区)1/年月日时分秒纳秒方式指定
ZonedDateTime time1 = ZonedDateTime.of(2023, 10, 1,
11, 12, 12, 0, ZoneId.of("Asia/Shanghai"));
System.out.println(time1);
//通过Instant + 时区的方式指定获取时间对象
Instant instant = Instant.ofEpochMilli(0L);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(time2);
//3.withXxx 修改时间系列的方法
ZonedDateTime time3 = time2.withYear(2000);
System.out.println(time3);
//4. 减少时间
ZonedDateTime time4 = time3.minusYears(1);
System.out.println(time4);
//5.增加时间
ZonedDateTime time5 = time4.plusYears(1);
System.out.println(time5);
注意:JDK8之后的时间类似字符串,不可变,变的是地址;
SimpleDateFormat
/*
static DateTimeFormatter ofPattern(格式) 获取格式对象
String format(时间对象) 按照指定方式格式化
*/
//获取时间对象
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
// 解析/格式化器
DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss EE a");
// 格式化
System.out.println(dtf1.format(time));
Calendar:
LocalDate:只获取日期,不包含时间
public static void main(String[] args) {
//1.获取当前时间的日历对象(包含 年月日)
LocalDate nowDate = LocalDate.now();
System.out.println("今天的日期:" + nowDate);
//2.获取指定的时间的日历对象
LocalDate ldDate = LocalDate.of(2023, 1, 1);
System.out.println("指定日期:" + ldDate);
System.out.println("=============================");
//3.get系列方法获取日历中的每一个属性值//获取年
int year = ldDate.getYear();
System.out.println("year: " + year);
//获取月//方式一:
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());
//方式二:
int month = ldDate.getMonthValue();
System.out.println("month: " + month);
//获取日
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);
//获取一年的第几天
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);
//获取星期
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());
//is开头的方法表示判断
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));
//with开头的方法表示修改,只能修改年月日
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);
//minus开头的方法表示减少,只能减少年月日
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);
//plus开头的方法表示增加,只能增加年月日
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println(plusLocalDate);
//-------------
// 判断今天是否是你的生日
LocalDate birDate = LocalDate.of(2000, 1, 1);
LocalDate nowDate1 = LocalDate.now();
MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
MonthDay nowMd = MonthDay.from(nowDate1);
System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));//今天是你的生日吗?
}
LocalTime:只获取时间,不包含日期
// 获取本地时间的日历对象。(包含 时分秒)
LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);
int hour = nowTime.getHour();//时
System.out.println("hour: " + hour);
int minute = nowTime.getMinute();//分
System.out.println("minute: " + minute);
int second = nowTime.getSecond();//秒
System.out.println("second:" + second);
int nano = nowTime.getNano();//纳秒
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));//时分
System.out.println(LocalTime.of(8, 20, 30));//时分秒
System.out.println(LocalTime.of(8, 20, 30, 150));//时分秒纳秒
LocalTime mTime = LocalTime.of(8, 20, 30, 150);
//is系列的方法
System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));
//with系列的方法,只能修改时、分、秒
System.out.println(nowTime.withHour(10));
//plus系列的方法,只能修改时、分、秒
System.out.println(nowTime.plusHours(10));
LocalDateTime :年、月、日、时、分、秒
// 当前时间的的日历对象(包含年月日时分秒)
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("今天是:" + nowDateTime);//今天是:
System.out.println(nowDateTime.getYear());//年
System.out.println(nowDateTime.getMonthValue());//月
System.out.println(nowDateTime.getDayOfMonth());//日
System.out.println(nowDateTime.getHour());//时
System.out.println(nowDateTime.getMinute());//分
System.out.println(nowDateTime.getSecond());//秒
System.out.println(nowDateTime.getNano());//纳秒
// 日:当年的第几天
System.out.println("dayofYear:" + nowDateTime.getDayOfYear());
//星期
System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());
//月份
System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());
LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);
LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());
有关时间间隔的三个类:
//Duration(秒,纳秒)
// 本地日期时间对象。
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
// 出生的日期时间对象
LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);
System.out.println(birthDate);
Duration duration = Duration.between(birthDate, today);//第二个参数减第一个参数
System.out.println("相差的时间间隔对象:" + duration);
System.out.println("============================================");
System.out.println(duration.toDays());//两个时间差的天数
System.out.println(duration.toHours());//两个时间差的小时数
System.out.println(duration.toMinutes());//两个时间差的分钟数
System.out.println(duration.toMillis());//两个时间差的毫秒数
System.out.println(duration.toNanos());//两个时间差的纳秒数
//Period(年月日)
// 当前本地 年月日
LocalDate today = LocalDate.now();
System.out.println(today);
// 生日的 年月日
LocalDate birthDate = LocalDate.of(2000, 1, 1);
System.out.println(birthDate);
Period period = Period.between(birthDate, today);//第二个参数减第一个参数
System.out.println("相差的时间间隔对象:" + period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.toTotalMonths());
//ChronoUnit 全部
// 当前时间
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
// 生日时间
LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1,0, 0, 0);
System.out.println(birthDate);
System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
包装类
Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于高效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,也就是把基本数据类型变成了引用数据类型,这也就是为什么集合只能存对象,需要存数字时候用到包装类;基本数据类型对应的包装类详见之前的一篇ArrayList,里面用到了包装类;
方法名 | 说明 |
---|---|
public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
public static Integer valueOf(String s) | 返回保存指定String值的 Integer 对象 |
static string tobinarystring(int i) | 得到二进制 |
static string tooctalstring(int i) | 得到八进制 |
static string toHexstring(int i) | 得到十六进制 |
static int parseInt(string s) | 将字符串类型的整数转成int类型的整数 |
类似BigDecimal和BigInteger,valueOf方法创建时候在-128~127之间有优化,这个范围内的数字会优先从底层数组里面拿;
创建Integer对象在JDK5以后像基本数据类型一样直接赋值就好;而且存在自动装拆箱,无需手动装拆箱,底层帮你干了;
Integer的成员方法:
方法名 | 说明 |
---|---|
public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
public static Integer valueOf(String s) | 返回保存指定String值的 Integer 对象 |
static string toBinaryString(int i) | 得到二进制 |
static string toOctalString(int i) | 得到八进制 |
static string toHexString(int i) | 得到十六进制 |
static int parseInt(string s) | 将字符串类型的整数转成int类型的整数 |
API相关练习
练习一:键盘录入一些1~100之间的整数,并添加到集合中。直到集合中所有数据和超过200为止。
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arrayList = new ArrayList<>();
//ctrl alt t 快捷包裹代码块
while (true) {
System.out.println("输入数字:");
int num = Integer.parseInt(sc.nextLine());
if (num > 100 || num < 0) {
System.out.println("重新输入!");
continue;
}
arrayList.add(num);
int sum = 0;
for (int i = 0; i < arrayList.size(); i++) {
sum = sum + arrayList.get(i);
}
if (sum >= 200) {
System.out.println("结束。");
break;
}
}
练习二:
自己实现parseInt方法的效果,将字符串形式的数据转成整数。
要求:字符串中只能是数字,不能有其他字符。最少一位,最多10位,不能以0开头。
//1.定义一个字符串
String str = "123";
//2.校验字符串
//习惯:会先把异常数据进行过滤,剩下来就是正常的数据。
if (!str.matches("[1-9]\\d{0,9}")) {
//错误的数据
System.out.println("数据格式有误");
} else {
//正确的数据
System.out.println("数据格式正确");
//3.定义一个变量表示最终的结果
int number = 0;
//4.遍历字符串得到里面的每一个字符
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i) - '0';//把每一位数字放到number当中 原理:利用字符串加减是用ASCII表实现的
number = number * 10 + c;
}
System.out.println(number);
}
练习三:
定义一个方法自己实现toBinaryString方法的效果,将一个十进制整数转成字符串表示的二进制。
int i = 100;
StringBuilder stringBuilder = new StringBuilder();
while (true) {
int j = (int) Math.floor(i / 2);
stringBuilder.append(i % 2);
if (j == 0){
stringBuilder = stringBuilder.reverse();
System.out.println(stringBuilder.toString());
break;
}
i = j;
}
练习四:
请使用代码实现计算你活了多少天,用JDK7和JDK8两种方式完成
public static void TimeJDK7(String time) throws ParseException {
//System.currentTimeMillis() 也可以获取当前时间
Date nowDate = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birDate = simpleDateFormat.parse(time);
System.out.println((nowDate.getTime() - birDate.getTime()) / 1000 / 3600 / 24);
}
public static void TimeJDK8(){
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = LocalDate.of(2002,1,1);
long chronoUnit = ChronoUnit.DAYS.between(localDate1,localDate);
System.out.println(chronoUnit);
}
练习五:判断闰年
//jdk7
//我们可以把时间设置为2000年3月1日
Calendar c = Calendar.getInstance();
c.set(2000, 2, 1);
//月份的范围:0~11
//再把日历往前减一天
c.add(Calendar.DAY_OF_MONTH, -1);
//看当前的时间是28号还是29号?
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(day);
//jdk8
//月份的范围:1~12
//设定时间为2000年的3月1日
LocalDate ld = LocalDate.of(2001, 3, 1);
//把时间往前减一天
LocalDate ld2 = ld.minusDays(1);
//获取这一天是一个月中的几号
int day2 = ld2.getDayOfMonth();
System.out.println(day2);
//true:闰年
//false:平年
System.out.println(ld.isLeapYear());