String strDate = "2022-12-31";
String strTime = "00:00:00";
String strDateTime = strDate + " " + strTime;
long timestamp = 1672416000000L;
DateTimeFormatter YY_M_D_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter YY_M_D = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
LocalDateTime localDateTime = LocalDateTime.parse(strDateTime, YY_M_D_HMS);
LocalDate localDate = LocalDate.parse(strDate, YY_M_D);
Date date = Date.from(LocalDateTime.parse(strDateTime, YY_M_D_HMS).atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime -> LocalDate:\t" + localDateTime.toLocalDate());
System.out.println("LocalDateTime -> Long:\t" + localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("LocalDateTime -> Date:\t" + Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
System.out.println("LocalDateTime -> String:\t" + localDateTime.format(YY_M_D_HMS));
System.out.println("LocalDate -> LocalDateTime:\t" + LocalDateTime.of(localDate, LocalTime.parse(strTime)));
System.out.println("LocalDate -> Long:\t" + localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.out.println("LocalDate -> Date:\t" + Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
System.out.println("LocalDate -> String:\t" + YY_M_D.format(localDate));
System.out.println("Date -> LocalDateTime:\t" + LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
System.out.println("Date -> Long:\t" + date.getTime());
System.out.println("Date -> LocalDateTime:\t" + LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());
System.out.println("Date -> String:\t" + sdf.format(date));
System.out.println("String -> LocalDateTime:\t" + LocalDateTime.parse(strDateTime, YY_M_D_HMS));
System.out.println("String -> LocalDate:\t" + LocalDateTime.parse(strDateTime, YY_M_D_HMS).toLocalDate());
System.out.println("String -> LocalDate:\t" + LocalDate.parse(strDate, YY_M_D));
System.out.println("String -> Date:\t" + Date.from(LocalDateTime.parse(strDateTime, YY_M_D_HMS).atZone(ZoneId.systemDefault()).toInstant()));
System.out.println("Long -> LocalDateTime:" + LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
System.out.println("Long -> LocalDate:\t" + LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());
// 日期比较
LocalDateTime now = LocalDateTime.now();
LocalDateTime pre = now.plusDays(-3);
LocalDateTime after = now.plusDays(3);
System.out.println(now.isAfter(pre) && after.isAfter(now));
// 获取差值
LocalDateTime startTime = LocalDateTime.of(2020, 12, 31, 0, 0, 0);
LocalDateTime endTime = LocalDateTime.of(2022, 12, 31, 0, 0, 0);
Duration duration = Duration.between(startTime, endTime);
System.out.println("相差天数:" + duration.toDays());
System.out.println("相差小时数:" + duration.toHours());
System.out.println("相差分钟数:" + duration.toMinutes());
System.out.println("相差秒数:" + duration.toSeconds());
System.out.println("相差毫秒数:" + duration.toMillis());
System.out.println("相差纳秒数:" + duration.toNanos());
标签:转换,System,Date,LocalDateTime,println,LocalDate,out
From: https://www.cnblogs.com/srczhang/p/17007159.html