LocalDate获取年月日
LocalDate today = LocalDate.now();
System.out.println("今天的日期:" + today);
System.out.println(
"年月日是:" + today.getYear() + "年" + today.getMonthValue() + "月" + today.getDayOfMonth()
+ "日");
LocalTime获取时分秒
LocalDateTime获取年月日时分秒
根据时区获取时间
LocalDateTime.now(ZoneId.of("UTC"))
获取三天后的UTC时间
LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plus(3,ChronoUnit.DAYS)
获取ZoneId
ZoneId.getAvailableZoneIds()
一、LocalDate相关
1.LocalDate获取某月第一天
LocalDate localDate = LocalDate.now();
localDate = localDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
2.LocalDate格式化
public String formatLocalDate(LocalDate date, String format) {
if (StringUtils.isBlank(format)) {
return date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
} else {
return date.format(DateTimeFormatter.ofPattern(format));
}
}
3.LocalDate转换为Instant
LocalDate localDate = LocalDate.now();
Instant instant = localDate.atStartOfDay().toInstant(ZoneOffset.UTC);
4.LocalDate转换为LocalDateTime
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = localDate.atStartOfDay();
二、LocalDateTime相关
1.LocalDateTime获取上月第一天
LocalDateTime localDateTime = LocalDateTime.now();
localDateTime = localDateTime.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
localDateTime = localDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
//2021-12-01T00:00
2.LocalDateTime格式化
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(fmt);
//2022-01-10 10:04:22
3.LocalDateTime转换为Instant
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime zdt = localDateTime.atZone(zoneId);
Instant instant = zdt.toInstant();
//2022-01-10T02:16:27.834392400Z
4.LocalDateTime转换为LocalDate
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
//2022-01-10
三、Instant相关
1.Instant转换为LocalDateTime
Instant instant = Instant.now();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
//2022-01-10T02:28:53.043126200
2.Instatnt转换为LocalDate
Instant instant = Instant.now();
LocalDate localDate = LocalDateTime.ofInstant(instant, ZoneId.of("UTC")).toLocalDate();
//2022-01-10
3.Instant格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")
.withLocale(Locale.CHINA)
.withZone(ZoneId.systemDefault());
String format = formatter.format(instant);
//2022-01-10 10:40:38
4.Instant获取下一日的结束时间
Instant now = Instant.now();
Instant expiredDate = now.plus(Duration.ofDays(1));
LocalDateTime localDateTime = LocalDateTime.ofInstant(expiredDate, ZoneId.of("Asia/Shanghai"));
localDateTime = localDateTime.with(LocalTime.MAX);
//2022-01-11T23:59:59.999999999
5.两个日期相差多少毫秒
Instant now = Instant.now();
Instant expiredDate = now.plus(Duration.ofDays(1));
long haomiao = ChronoUnit.MILLIS.between(Instant.now(), expiredDate);
//86399998
四、Date String相关
1.日期字符串转换为LocalDateTime
String dateString = "2022-01-10 10:40:38";
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
LocalDateTime ldt = LocalDateTime.parse(dateString, f);
//2022-01-10T10:40:38
2.日期字符串转换为LocalDate
String dateString = "2022-01-10 10:40:38";
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
LocalDateTime ldt = LocalDateTime.parse(dateString, f);
LocalDate localDate = ldt.toLocalDate();
//2022-01-10
3.日期字符串转换为Instant
String dateString = "2022-01-10 10:40:38";
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
LocalDateTime ldt = LocalDateTime.parse(dateString, f);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime zonedDateTime = ldt.atZone(zoneId);
Instant instant = zonedDateTime.toInstant();
//2022-01-10T02:40:38Z