Java中YearMonth类简介说明
下文笔者讲述java中YearMonth类的功能简介说明,如下所示
YearMonth类简介
YearMonth类 是Java8中的一个时间日期API中的组合类 它表示一个月份的年份
YearMonth类示例
import java.time.YearMonth;
public class TestClass {
public static void main(String[] args) {
// 获取当前年月
YearMonth current = YearMonth.now();
System.out.println("当前年月:" + current);
// 获取指定年月
YearMonth specified = YearMonth.of(2022, 2);
System.out.println("指定年月:" + specified);
// 获取当前年月的天数
int days = current.lengthOfMonth();
System.out.println("当前年月的天数:" + days);
// 获取指定年月的天数
int days2 = specified.lengthOfMonth();
System.out.println("指定年月的天数:" + days2);
// 增加或减少年份
YearMonth addYear = current.plusYears(1);
YearMonth minusYear = current.minusYears(1);
System.out.println("当前年月增加1年:" + addYear);
System.out.println("当前年月减少1年:" + minusYear);
// 增加或减少月份
YearMonth addMonth = current.plusMonths(3);
YearMonth minusMonth = current.minusMonths(3);
System.out.println("当前年月增加3个月:" + addMonth);
System.out.println("当前年月减少3个月:" + minusMonth);
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


