一、LocalDateTime
1. 获取目前时间:
LocalDateTime localDateTime = LocalDateTime.now();//会把世界标准时间转换为本时区的时间,但是时区信息会被丢弃
2. LocalDateTime
互相转换String
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
System.out.println("String类型的时间转成LocalDateTime:"+ldt);
3. LocalDateTime
转换为Instant
Instant localDateTimeToInstant = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();
控制台结果:
LocalDateTime转成String类型的时间:2017-09-30 10:40:06
String类型的时间转成LocalDateTime:2017-09-28T17:07:05
二、 Instant
1. 获取目前时间:
Instant instant = Instant.now();//世界标准时间
2. Instant
互相转换String
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault())
.withLocale(Locale.CHINA );
Instant instant = Instant.now();
String output = formatter.format( instant );
Instant instant = LocalDateTime.parse("2017-06-23 15:34:20", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault())
.withLocale(Locale.CHINA ))
.toInstant(ZoneOffset.UTC)
3. Instant
转换为 LocalDateTime
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
三、Date
,Instant
, LocalDate
,LocalTime
,LocalDateTime
Java 8
中 java.util.Date
类新增了两个方法,分别是from(Instant instant)
和toInstant()
方法
// Obtains an instance of Date from an Instant object.
public static Date from(Instant instant) {
try {
return new Date(instant.toEpochMilli());
} catch (ArithmeticException ex) {
throw new IllegalArgumentException(ex);
}
}
// Converts this Date object to an Instant.
public Instant toInstant() {
return Instant.ofEpochMilli(getTime());
}
这两个方法使我们可以方便的实现将旧的日期类转换为新的日期类,具体思路都是通过Instant
当中介,然后通过Instant
来创建LocalDateTime
(这个类可以很容易获取LocalDate
和LocalTime
),新的日期类转旧的也是如此,将新的先转成LocalDateTime
,然后获取Instant
,接着转成Date
,具体实现细节如下:
一、Date
转换为LocalDateTime
// 01. java.util.Date --> java.time.LocalDateTime
public void UDateToLocalDateTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
}
二、Date
转换为LocalDate
// 02. java.util.Date --> java.time.LocalDate
public void UDateToLocalDate() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate = localDateTime.toLocalDate();
}
三、Date
转换为LocalTime
// 03. java.util.Date --> java.time.LocalTime
public void UDateToLocalTime() {
java.util.Date date = new java.util.Date();
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalTime localTime = localDateTime.toLocalTime();
}
四、LocalDateTime
转换为 Date
// 04. java.time.LocalDateTime --> java.util.Date
public void LocalDateTimeToUdate() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
五、LocalDate
转换为 Date
// 05. java.time.LocalDate --> java.util.Date
public void LocalDateToUdate() {
LocalDate localDate = LocalDate.now();
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
六、LocalTime
转换为 Date
// 06. java.time.LocalTime --> java.util.Date
public void LocalTimeToUdate() {
LocalTime localTime = LocalTime.now();
LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
java.util.Date date = Date.from(instant);
}
参考文章:
LocalDateTime与String日期互相转换
java8 Date Localdatetime instant 相互转化(转) 及当天的最大/最小时间
Java中的时间与时区
java日期互转:LocalDateTime、String、TimeStamp、Long、Instant、Date