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.Date
methods toInstant()
to Date
convert an object to Instant
an object, then use Instant
an object's atZone()
method to convert it to ZonedDateTime
an object, and finally use ZonedDateTime
an object's toLocalDate()
method to convert it to LocalDate
an 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.Date
methods toInstant()
to Date
convert an object to Instant
an object, then use Instant
an object's atOffset()
method to convert it to OffsetDateTime
an object, and finally use OffsetDateTime
an object's toLocalDate()
method to convert it to LocalDate
an 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.Calendar
a class to convert the Date object to LocalDate
an object. First, you need to Date
convert the object into Calendar
an object, then use Calendar
the object's get()
methods to obtain information such as year, month, day, etc., and finally use LocalDate.of()
the methods to convert it into LocalDate
an 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 SimpleDateFormat
classes to Date对
convert objects into LocalDate
objects. First you need to create an SimpleDateFormat
object, then use format()
the method to Date
format the object into the specified date format, and finally use LocalDate.parse()
the method to convert it into LocalDate
an 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.Date
methods toInstant()
to Date
convert an object to Instant
an object, then use Instant
an object's atZone()
method to convert it to ZonedDateTime
an object, and finally use ZonedDateTime
an object's toLocalTime()
method to convert it to LocalDate
an 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.Date
methods toInstant()
to Date
convert an object to Instant
an object, then use Instant
an object's atOffset()
method to convert it to OffsetDateTime
an object, and finally use OffsetDateTime
an object's toLocalTime()
method to convert it to LocalDate
an object.
// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalTime
LocalTime localTime = date.toInstant().atOffset(ZoneOffset.UTC).toLocalTime();
3. Use SimpleDateFormat
convert Date
object to LocalTime
object.
// 创建一个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 Calendar
classes to Date
convert objects to LocalDate
objects.
// 创建一个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 .Instant
LocalDateTime
// 创建一个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.Date
methods toInstant()
to Date
convert an object to Instant
an object, then use Instant
an object's atOffset()
method to convert it to OffsetDateTime
an object, and finally use OffsetDateTime
an object's toLocalDateTime()
method to convert it to LocalDateTime
an 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 Date
convert the object to ZonedDateTime
an object, and then use toLocalDateTime()
the method to convert it to LocalDateTime
an object.
// 创建一个java.util.Date对象
Date date = new Date();
// 将java.util.Date转换为java.time.LocalDateTime
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
3. Use SimpleDateFormat
convert Date
object to LocalDateTime
object.
// 创建一个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 Calendar
convert Date
object to LocalDateTime
object.
// 创建一个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 Instant
convert object and specified time zone to ZonedDateTime
object.
// 创建一个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 Instant
convert the object and the specified time zone to ZonedDateTime
the 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 ZonedDateTime
convert the object to OffsetDateTime
Object, a subclass of OffsetDateTime
Object ZonedDateTime
, which represents a datetime with an offset. When converting, ZonedDateTime
the 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 beZoneId.of(String)
specified by using the method.