以下是关于Java时间类型的内容,涵盖了
LocalDate
与
Date
类型的互转、字符串格式化以及其他常见时间类型的转换。
Java时间类型详解:LocalDate与Date互转及字符串格式化
在Java中,时间处理是一个常见的需求。随着Java 8引入了新的日期时间API(java.time
包),开发者可以更方便地处理日期和时间。本文将详细介绍 LocalDate
与 Date
类型的互转、字符串格式化以及其他常见时间类型的转换。
一、LocalDate
与 Date
类型互转
1. Date
转 LocalDate
Date
是Java早期的日期时间类,而 LocalDate
是Java 8引入的日期类。我们可以通过以下方式将 Date
转换为 LocalDate
:
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDate {
public static void main(String[] args) {
// 获取当前Date对象
Date nowDate = new Date();
// 将Date转换为LocalDate
LocalDate localDate = nowDate.toInstant() // 将Date转换为Instant
.atZone(ZoneId.systemDefault()) // 指定时区
.toLocalDate(); // 转换为LocalDate
System.out.println("Date: " + nowDate);
System.out.println("LocalDate: " + localDate);
}
}
输出示例:
Date: Mon Oct 09 12:34:56 CST 2023
LocalDate: 2023-10-09
2. LocalDate
转 Date
将 LocalDate
转换为 Date
需要借助 ZonedDateTime
或 Instant
:
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateToDate {
public static void main(String[] args) {
// 获取当前LocalDate对象
LocalDate localDate = LocalDate.now();
// 将LocalDate转换为Date
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDate: " + localDate);
System.out.println("Date: " + date);
}
}
输出示例:
LocalDate: 2023-10-09
Date: Mon Oct 09 00:00:00 CST 2023
二、字符串格式化与解析
1. LocalDate
与字符串互转
(1)LocalDate
转字符串
使用 DateTimeFormatter
可以将 LocalDate
格式化为字符串:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateToString {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
// 定义格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 格式化LocalDate为字符串
String dateString = localDate.format(formatter);
System.out.println("LocalDate: " + localDate);
System.out.println("Formatted String: " + dateString);
}
}
输出示例:
LocalDate: 2023-10-09
Formatted String: 2023-10-09
(2)字符串转 LocalDate
将字符串解析为 LocalDate
:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
public static void main(String[] args) {
String dateString = "2023-10-09";
// 定义格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 解析字符串为LocalDate
LocalDate localDate = LocalDate.parse(dateString, formatter);
System.out.println("String: " + dateString);
System.out.println("LocalDate: " + localDate);
}
}
输出示例:
String: 2023-10-09
LocalDate: 2023-10-09
2. Date
与字符串互转
(1)Date
转字符串
使用 SimpleDateFormat
可以将 Date
格式化为字符串:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateToString {
public static void main(String[] args) {
Date nowDate = new Date();
// 定义格式化器
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 格式化Date为字符串
String dateString = formatter.format(nowDate);
System.out.println("Date: " + nowDate);
System.out.println("Formatted String: " + dateString);
}
}
输出示例:
Date: Mon Oct 09 12:34:56 CST 2023
Formatted String: 2023-10-09 12:34:56
(2)字符串转 Date
将字符串解析为 Date
:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args) throws ParseException {
String dateString = "2023-10-09 12:34:56";
// 定义格式化器
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 解析字符串为Date
Date date = formatter.parse(dateString);
System.out.println("String: " + dateString);
System.out.println("Date: " + date);
}
}
输出示例:
String: 2023-10-09 12:34:56
Date: Mon Oct 09 12:34:56 CST 2023
三、其他常见时间类型转换
1. LocalDateTime
与 Date
互转
(1)Date
转 LocalDateTime
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalDateTime {
public static void main(String[] args) {
Date nowDate = new Date();
// 将Date转换为LocalDateTime
LocalDateTime localDateTime = nowDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
System.out.println("Date: " + nowDate);
System.out.println("LocalDateTime: " + localDateTime);
}
}
(2)LocalDateTime
转 Date
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalDateTimeToDate {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
// 将LocalDateTime转换为Date
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime: " + localDateTime);
System.out.println("Date: " + date);
}
}
2. LocalTime
与 Date
互转
(1)Date
转 LocalTime
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class DateToLocalTime {
public static void main(String[] args) {
Date nowDate = new Date();
// 将Date转换为LocalTime
LocalTime localTime = nowDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalTime();
System.out.println("Date: " + nowDate);
System.out.println("LocalTime: " + localTime);
}
}
(2)LocalTime
转 Date
由于 LocalTime
只包含时间信息,转换为 Date
时需要结合日期:
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;
public class LocalTimeToDate {
public static void main(String[] args) {
LocalTime localTime = LocalTime.now();
// 将LocalTime转换为Date(需要结合日期)
Date date = Date.from(LocalDate.now()
.atTime(localTime)
.atZone(ZoneId.systemDefault())
.toInstant());
System.out.println("LocalTime: " + localTime);
System.out.println("Date: " + date);
}
}