Hutool工具类 pom
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.12</version>
</dependency>
public static void main(String[] args) {
DateTime dateTime = new DateTime("2021-9-14 09:11:23", "yyyy-MM-dd HH:mm:ss");
// 获取日期成员(年、季度、月、日)
int year = dateTime.year();// 获取年
System.out.println("year = " + year);
int month = dateTime.month();//获取 月份
System.out.println("month = " + month);
int dayOfWeek = dateTime.dayOfWeek();// 获取星期几
System.out.println("dayOfWeek = " + dayOfWeek);
int dayOfMonth = dateTime.dayOfMonth();// 获取在本月的第几天
System.out.println("dayOfMonth = " + dayOfMonth);
// 偏移日期
DateTime dateTime1 = dateTime.offset(DateField.DAY_OF_MONTH, -1); //向前退一个月
System.out.println(dateTime1.toString());
}