1、Java8常用的日期函数处理,用起来比较方便
import java.time.LocalDate;
public class NewClass {
public static void main(String[] args) {
//当前日期 2023-02-15
LocalDate localDate = LocalDate.now();
//昨天 2023-02-14
LocalDate preDay=localDate.plusDays(-1);
//明天 2023-02-16
LocalDate nextDay=localDate.plusDays(1);
//今天周三 对应下周三的日期 2023-02-22
LocalDate nextWeek=localDate.plusWeeks(1);
//今天周三 对应上周三的日期 2023-02-08
LocalDate preWeek=localDate.plusWeeks(-1);
//下个月 2023-03-15
LocalDate nextMonth=localDate.plusMonths(1);
//上个月 2023-01-15
LocalDate preMonth=localDate.plusMonths(-1);
//明年 2024-02-15
LocalDate nextYear=localDate.plusYears(1);
//去年 2022-02-15
LocalDate preYear=localDate.plusYears(-1);
}
}
2、字符串转成日期时间对象
public class NewClass {
public static void main(String[] args) {
String datestr="2023-02-15";
//字符串转成日期时间对象
LocalDate localDate=LocalDate.parse(datestr);
System.out.println(localDate);
}
}