📅  最后修改于: 2020-10-01 06:36:51             🧑  作者: Mango
Java LocalDate类是一个不可变的类,它以默认格式yyyy-MM-dd表示Date。它继承了Object类并实现了ChronoLocalDate接口
让我们看看java.time.LocalDate类的声明。
public final class LocalDate extends Object
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable
Method | Description |
---|---|
LocalDateTime atTime(int hour, int minute) | It is used to combine this date with a time to create a LocalDateTime. |
int compareTo(ChronoLocalDate other) | It is used to compares this date to another date. |
boolean equals(Object obj) | It is used to check if this date is equal to another date. |
String format(DateTimeFormatter formatter) | It is used to format this date using the specified formatter. |
int get(TemporalField field) | It is used to get the value of the specified field from this date as an int. |
boolean isLeapYear() | It is used to check if the year is a leap year, according to the ISO proleptic calendar system rules. |
LocalDate minusDays(long daysToSubtract) | It is used to return a copy of this LocalDate with the specified number of days subtracted. |
LocalDate minusMonths(long monthsToSubtract) | It is used to return a copy of this LocalDate with the specified number of months subtracted. |
static LocalDate now() | It is used to obtain the current date from the system clock in the default time-zone. |
LocalDate plusDays(long daysToAdd) | It is used to return a copy of this LocalDate with the specified number of days added. |
LocalDateplusMonths(long monthsToAdd) | It is used to return a copy of this LocalDate with the specified number of months added. |
import java.time.LocalDate;
public class LocalDateExample1 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate yesterday = date.minusDays(1);
LocalDate tomorrow = yesterday.plusDays(2);
System.out.println("Today date: "+date);
System.out.println("Yesterday date: "+yesterday);
System.out.println("Tommorow date: "+tomorrow);
}
}
输出:
Today date: 2017-01-13
Yesterday date: 2017-01-12
Tommorow date: 2017-01-14
import java.time.LocalDate;
public class LocalDateExample2 {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2017, 1, 13);
System.out.println(date1.isLeapYear());
LocalDate date2 = LocalDate.of(2016, 9, 23);
System.out.println(date2.isLeapYear());
}
}
输出:
false
true
import java.time.*;
public class LocalDateExample3 {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2017, 1, 13);
LocalDateTime datetime = date.atTime(1,50,9);
System.out.println(datetime);
}
}
输出:
2017-01-13T01:50:09