文章目录
- LocalDate
- plus
- minus
- 比较日期
- LocalDate、Date、LocalDateTime、时间戳的转换
LocalDate
plus
LocalDate localDate2 = localDate1.plus(15, ChronoUnit.DAYS);
LocalDate localDate2 = localDate1.plus(Period.ofDays(15));
minus
LocalDate localDate2 = localDate1.minus(15, ChronoUnit.DAYS);
LocalDate localDate2 = localDate1.minus(Period.ofDays(15));
比较日期
isAfter(ChronoLocalDate other): 检查此日期是否在给定日期之后。
isBefore(ChronoLocalDate other): 检查此日期是否在给定日期之前。
isEqual(ChronoLocalDate other): 检查此日期是否等于给定日期。
示例
import java.time.LocalDate;
public class LocalDateDemo {
public static void main(String[] args) {
LocalDate localDate1 = LocalDate.parse("2018-03-18");
LocalDate localDate2 = LocalDate.parse("2018-05-12");
System.out.println(localDate1.isAfter(localDate2));
System.out.println(localDate1.isBefore(localDate2));
System.out.println(localDate1.isEqual(localDate2));
}
}
输出
false
true
false
LocalDate、Date、LocalDateTime、时间戳的转换
1.LocalDate转Date
LocalDate nowLocalDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
2.LocalDateTime转Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());
3.Date转LocalDateTime(LocalDate)
Date date =newDate();
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
4.LocalDate转时间戳
LocalDate localDate = LocalDate.now();
longtimestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
5.LocalDateTime转时间戳
LocalDateTime localDateTime = LocalDateTime.now();
longtimestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
6.时间戳转LocalDateTime(LocalDate)
longtimestamp = System.currentTimeMillis();
LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
链接:https://blog.csdn.net/qq_31635851/article/details/117880835
链接:https://blog.csdn.net/qq_31635851/article/details/117880835