LocalDate相关知识点

1、LocalDate相关操作:

日期比较大小用compareTo

int result = a.compareTo(b);

result = 0 : a = b

result = 1:a > b

result = -1:a < b

LocalDate date = LocalDate.now();
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter dtf0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM");
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy");
String date1 = date.format(dtf1); //2023-06-01
String date2 = date.format(dtf2); //2023-06
String date3 = date.format(dtf3); //2023
String time0 = time.format(dtf0); //2023-06-01 20:13:51
String time1 = time.format(dtf1); //2023-06-01
String time2 = time.format(dtf2); //2023-06
String time3 = time.format(dtf3); //2023
        
//格式化年月
YearMonth yearMonth = YearMonth.parse("2023-02", dtf2); //2023-02

//获取当前月的最后一天
LocalDate currentDate = yearMonth.atEndOfMonth(); //2023-02-28

//格式化为String
String format = currentDate.format(dtf1); //2023-02-28

//获取当前月的第一天
LocalDate firstDay = currentDate.with(TemporalAdjusters.firstDayOfMonth()); //2023-02-01

//获取当前月下月的第一天
LocalDate nextMonthFirstDay = firstDay.minusMonths(-1); //2023-03-01

//获取当前月份
YearMonth currentMonth = YearMonth.now(); //2023-06

//格式化当前月份
String currentMonth = yearMonth.format(dtf2); //2023-06

//获取当前年份
Year currentYear = Year.now(); //2023
          

2、Date相关操作

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值