Instant timestampDuration
duration, time difference
LocalDate only contains date, for example: 2018-09-24
LocalTime only contains time, for example: 10:32:10
LocalDateTime contains date and time, for example: 2018-09-24 10:32:10
Peroid time period
ZoneOffset time zone offset, for example: +8:00
ZonedDateTime date and time with time zone
Clock clock, which can be used to obtain the current timestamp
java.time.format.DateTimeFormatter time formatting class
Get the current time LocalDateTime time = LocalDateTime.now();
Date format: DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Convert current time to String in java8: String tm = time.format(formatter);
Convert Date time to String: Date date = new Date();
Instant instant =date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime =LocalDateTime.ofInstant(instant,zoneId);
String tm = formatter.format(localDateTime);
Convert String to Date type: String tm8="2020-11-11 08:00:00";
LocalDateTime localDateTime=LocalDateTime.parse(tm8,formatter);
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zoneId).toInstant();
Date btm = Date.from(instant);
LocalDate 转Date类型:LocalDate localDate =LocalDate.now();
ZonedDateTime zonedDateTime = dateTime.atStartOfDay(ZoneId.systemDefault());
Instant instant = zonedDateTime.toInstant();
Date btm = Date.from(instant);
If it is a LocalDate of type string, you only need to convert the String to LocalDate, such as
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate dateTime = LocalDate.parse(stringtm, dateTimeFormatter);
LocalDate.parse(stringtm,dateTimeFormatter );
Date转LocalDate:
Date date = new Date();
Instant instant = date.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
LocalDate localDate = zdt.toLocalDate();
Earliest and latest time of day:
LocalDateTime startTm = LocalDateTime.of(LocalDate.now(),LocalTime.MIN);//2020-11-11 00:00:00
LocalDateTime endTm = LocalDateTime.of(LocalDate.now(),LocalTime.MAX);//2020-11-11 23:59:59
0:00 on the first day of the month to 23:59:59 on the last day of the month
DateTimeFormatter da = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime monthTM = LocalDateTime.of(LocalDate.parse(tm + "-01", da), LocalTime.MIN); //tm is a string type 2020-11
LocalDateTime min = monthTM.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime max = monthTM.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
From 0:00 on the first day of the year to 23:59:59 on the last day of the year
DateTimeFormatter da = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime monthY = LocalDateTime.of(LocalDate.parse(tm + "-01-01", da),LocalTime.MIN); //tm is a string 2020
LocalDateTime min = monthY.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
LocalDateTime max = monthY.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);
Encountered a problem, when converting the above min and max to Date type, ZoneId zoneId = ZoneId.systemDefault(); Instant Sinstant = min.atZone(zoneId).toInstant(); Instant Einstant = max.atZone(zoneId) .toInstant(); The value here of Instant is 8 hours less than min, max. When converted to Date, 8 hours are automatically filled in. Date startTime = Date.from(Sinstant); Date endTime = Date.from(Einstant); The time here returns to normal, which should be related to the time zone involved in the ZoneId.