import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; public static void main(String[] args) { // LocalDateTime转字符串(true) DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("LocalDateTime转字符串:" + localDateTime.format(formatter)); // 将时间字符串转自定义格式的LocalDateTime String timeString = "2020-10-22 15:22:54"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime parse = LocalDateTime.parse(timeString, dateTimeFormatter); System.out.println("字符串转LocalDateTime:" + parse.format(dateTimeFormatter)); // 时间戳转LocalDateTime(true) long time = new Date().getTime(); System.out.println("java时间戳:" + time); DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Instant instant = Instant.ofEpochMilli(time); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant, zoneId); System.out.println("时间戳转LocalDateTime:" + localDateTime1.format(dateTimeFormatter3)); // LocalDateTime转时间戳(true) LocalDateTime localDateTime2 = LocalDateTime.now(); ZoneId zoneId1 = ZoneId.systemDefault(); Instant instant1 = localDateTime2.atZone(zoneId1).toInstant(); System.out.println("LocalDateTime转时间戳:" + instant1.toEpochMilli()); // LocalDate转字符串 DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.now(); System.out.println("LocalDate转字符串:" + localDate.format(formatter1)); // 将时间字符串转自定义格式的LocalDate String timeString2 = "2020-10-22"; DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); System.out.println("字符串转LocalDate:" + LocalDate.parse(timeString2, dateTimeFormatter1)); // 时间戳转LocalDate long time1 = new Date().getTime(); System.out.println("java时间戳:" + time1); Instant instant2 = Instant.ofEpochMilli(time1); ZoneId zoneId2 = ZoneId.systemDefault(); System.out.println("时间戳转LocalDate:" + instant2.atZone(zoneId2).toLocalDate()); // LocalDate转时间戳 LocalDate localDate2 = LocalDate.now(); ZoneId zoneId3 = ZoneId.systemDefault(); System.out.println("LocalDate转时间戳:" + localDate2.atStartOfDay(zoneId3).toInstant().toEpochMilli());
// LocalDate之间相差几天
LocalDate.toEpochDay() - LocalDate.toEpochDay() = 1 ; 表明:一个是1号 一个是2
// LocalDateTime转LocalDate DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDateTime localDateTime3 = LocalDateTime.now(); System.out.println("LocalDateTime转LocalDate:"+localDateTime3.format(dateTimeFormatter2)); LocalDate localDate1 = localDateTime3.toLocalDate(); System.out.println("LocalDateTime转LocalDate:"+localDate1.format(dateTimeFormatter4));
LocalDate today = LocalDate.now(); // LocalDate 算本周开始日期和本周结束日期 LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 算本月开始和结束日期 LocalDate firstDay = today.with(TemporalAdjusters.firstDayOfMonth()); LocalDate endDay = today.with(TemporalAdjusters.lastDayOfMonth());
}