LocalDate库的一些函数和例子
看<<Java核心技术>>类章节时看到了关于LocalDate的例子,就想着记录一些这个库的一些函数
函数
- LocalDate.now():获取当前日期
// LocalDate.now():获取当前的日期,返回一个LocalDate对象,输出形式为yyyy-MM-dd
LocalDate date = LocalDate.now();
System.out.println(date);
输出结果:
- LocalDate.of(int year, int month, int dayOfMonth)
// LocalDate.of(int year, int month, int dayofMonth):返回当前的日期,返回一个LocalDate对象,输出形式为yyyy-MM-dd
LocalDate date2 = LocalDate.of(2000, 7, 30);
System.out.println(date2);
输出结果:
- 获取一些年份信息:
// .getYear()/.lengthOfYear()/.isleapYear():返回localDate对象的年份/返回当前年份的天数/判断是否为闰年
int year = date1.getYear();
int dayOfYear = date1.lengthOfYear();
boolean isLeapYear = date1.isLeapYear();
System.out.println("当前的年份:" + year);
System.out.println("今年有的天数:" + dayOfYear);
System.out.println("今年是否为闰年:" + isLeapYear); // 天数和闰年好像意义有点相似?ㄟ( ▔, ▔ )ㄏ
输出结果: