Date class to Java8 new time class is enough to read this article, Date to LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime methods!

Date to Java8 new time class

1. Date to LocalDate:

LocalDate represents the date without time and time zone information, such as "2022-04-22".

1. Use java.util.Datemethods toInstant()to Dateconvert an object to Instantan object, then use Instantan object's atZone()method to convert it to ZonedDateTimean object, and finally use ZonedDateTimean object's toLocalDate()method to convert it to LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDate
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

2. Use java.util.Datemethods toInstant()to Dateconvert an object to Instantan object, then use Instantan object's atOffset()method to convert it to OffsetDateTimean object, and finally use OffsetDateTimean object's toLocalDate()method to convert it to LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDate
LocalDate localDate = date.toInstant().atOffset(ZoneOffset.UTC).toLocalDate();

3. Use java.util.Calendara class to convert the Date object to LocalDatean object. First, you need to Dateconvert the object into Calendaran object, then use Calendarthe object's get()methods to obtain information such as year, month, day, etc., and finally use LocalDate.of()the methods to convert it into LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDate
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
LocalDate localDate = LocalDate.of(year, month, day);

4. Use SimpleDateFormatclasses to Date对convert objects into LocalDateobjects. First you need to create an SimpleDateFormatobject, then use format()the method to Dateformat the object into the specified date format, and finally use LocalDate.parse()the method to convert it into LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDate
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(date);
LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd"));

2. Date to LocalTime:

LocalTime represents time without date and time zone information, such as "09:30:00".

1. Use java.util.Datemethods toInstant()to Dateconvert an object to Instantan object, then use Instantan object's atZone()method to convert it to ZonedDateTimean object, and finally use ZonedDateTimean object's toLocalTime()method to convert it to LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
LocalTime localTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();

2. Use java.util.Datemethods toInstant()to Dateconvert an object to Instantan object, then use Instantan object's atOffset()method to convert it to OffsetDateTimean object, and finally use OffsetDateTimean object's toLocalTime()method to convert it to LocalDatean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
LocalTime localTime = date.toInstant().atOffset(ZoneOffset.UTC).toLocalTime();

3. Use SimpleDateFormatconvert Dateobject to LocalTimeobject.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
LocalTime localTime = LocalTime.parse(sdf.format(date));

4. Use Calendarclasses to Dateconvert objects to LocalDateobjects.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
LocalTime localTime = LocalTime.of(hour, minute, second);

3. Date to LocalDateTime:

LocalDateTime represents date and time without time zone information, such as "2022-04-22T09:30:00".

1. Convert an object to an object using ofInstant()a method .InstantLocalDateTime

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

2. Use java.util.Datemethods toInstant()to Dateconvert an object to Instantan object, then use Instantan object's atOffset()method to convert it to OffsetDateTimean object, and finally use OffsetDateTimean object's toLocalDateTime()method to convert it to LocalDateTimean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(localDateTime);

2. First Dateconvert the object to ZonedDateTimean object, and then use toLocalDateTime()the method to convert it to LocalDateTimean object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDateTime
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

3. Use SimpleDateFormatconvert Dateobject to LocalDateTimeobject.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDateTime
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 根据格式化字符串创建DateTimeFormatter对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.parse(sdf.format(date), formatter);

4. Use Calendarconvert Dateobject to LocalDateTimeobject.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDateTime
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
LocalDateTime localDateTime = LocalDateTime.of(
    calendar.get(Calendar.YEAR),
    calendar.get(Calendar.MONTH) + 1,
    calendar.get(Calendar.DAY_OF_MONTH),
    calendar.get(Calendar.HOUR_OF_DAY),
    calendar.get(Calendar.MINUTE),
    calendar.get(Calendar.SECOND),
    calendar.get(Calendar.MILLISECOND) * 1000000
);

4. Date to ZonedDateTime:

ZonedDateTime represents date and time with time zone, for example "2022-04-22T09:30:00+08:00[GMT+08:00]".

1. Use atZone()method to Instantconvert object and specified time zone to ZonedDateTimeobject.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.ZonedDateTime
ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault());

2. Use ofInstant()the method to Instantconvert the object and the specified time zone to ZonedDateTimethe object.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());

5. Date to OffsetDateTime:

OffsetDateTime represents a date and time with an offset, such as "2022-04-22T09:30:00+08:00".

1. Call toOffsetDateTime()the method to ZonedDateTimeconvert the object to OffsetDateTimeObject, a subclass of OffsetDateTimeObject ZonedDateTime, which represents a datetime with an offset. When converting, ZonedDateTimethe time zone information of the object will be automatically converted into the corresponding offset information.

// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.OffsetDateTime
OffsetDateTime offsetDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();

In the above sample code, the default time zone information of the system is used ZoneId.systemDefault()to specify other time zone information, which can be ZoneId.of(String)specified by using the method.

Guess you like

Origin blog.csdn.net/TangBoBoa/article/details/130294701