Use of JDK8-LocalDate

Introduction

Since Java 8, many new features have been added to Java. One of the most common and practical is the date processing class-LocalDate.

There are three main types of newly added date jars:

java.time.LocalDate -> only deal with year, month and day

java.time.LocalTime -> Only deal with hours, minutes, seconds and nanoseconds

java.time.LocalDateTime -> can handle year, month, day and hour, minute and second at the same time

Date operation

//获取当前日期
LocalDate.now();
//获取当前日期的前一月
LocalDateTime begin = end.plus(-1L, ChronoUnit.MONTHS);
//获取当月的第一天
LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
//获取当月的最后一天
LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
//获取当月的第二天
LocalDate.now().withDayOfMonth(2);

LocalDateTime

Get the current hour, minute, and second converted to yyyy-MM-dd HH:mm:ss format

LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))

Guess you like

Origin blog.csdn.net/weixin_44642403/article/details/109286950