Java 8 新的日期时间 API

1、LocalDate 使用示例

LocalDate localDate = LocalDate.now();// 获取当前年月日
LocalDate localDate1 = LocalDate.of(2019, 9, 10);// 构造指定的年月日
System.out.println("localDate: " + localDate + "  localDate1: " + localDate1);

int year = localDate.getYear();
int year1 = localDate.get(ChronoField.YEAR);
System.out.println("year: " + year + "  year1: " + year1);

Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
System.out.println("month: " + month + "  month1: " + month1);

int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
System.out.println("day: " + day + "  day1: " + day1);

DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
System.out.println("dayOfWeek: " + dayOfWeek + "  dayOfWeek1: " + dayOfWeek1);

运行结果

localDate: 2019-11-21  localDate1: 2019-09-10
year: 2019  year1: 2019
month: NOVEMBER  month1: 11
day: 21  day1: 21
dayOfWeek: THURSDAY  dayOfWeek1: 4

2、LocalTime 使用示例

// 创建LocalTime
LocalTime localTime = LocalTime.now();
LocalTime localTime1 = LocalTime.of(13, 51, 10);
System.out.println("localTime: " + localTime + " localTime1: " + localTime1);
// 获取小时
int hour = localTime.getHour();
int hour1 = localTime.get(ChronoField.HOUR_OF_DAY);
System.out.println("hour: " + hour + " hour1: " + hour1);
// 获取分
int minute = localTime.getMinute();
int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR);
System.out.println("minute: " + minute + " minute1: " + minute1);
// 获取秒
int second = localTime.getSecond();
int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
System.out.println("second: " + second + " second1: " + second1);

运行结果

localTime: 10:06:26.892 localTime1: 13:51:10
hour: 10 hour1: 10
minute: 6 minute1: 6
second: 26 second1: 26

3、LocalDateTime 使用示例

// 创建LocalDateTime
LocalDate localDate = LocalDate.now();
System.out.println("localDate: " + localDate);

LocalTime localTime = LocalTime.now();
System.out.println("localTime: " + localTime);

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("localDateTime: " + localDateTime);

LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
System.out.println("localDateTime1: " + localDateTime1);

LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
System.out.println("localDateTime2: " + localDateTime2);

LocalDateTime localDateTime3 = localDate.atTime(localTime);
System.out.println("localDateTime3: " + localDateTime3);

LocalDateTime localDateTime4 = localTime.atDate(localDate);
System.out.println("localDateTime4: " + localDateTime4);

// 获取LocalDate
LocalDate localDate2 = localDateTime.toLocalDate();
System.out.println("localDate2: " + localDate2);

// 获取LocalTime
LocalTime localTime2 = localDateTime.toLocalTime();
System.out.println("localTime2: " + localTime2);

运行结果

localDate: 2019-11-21
localTime: 10:07:14.198
localDateTime: 2019-11-21T10:07:14.198
localDateTime1: 2019-09-10T14:46:56
localDateTime2: 2019-11-21T10:07:14.198
localDateTime3: 2019-11-21T10:07:14.198
localDateTime4: 2019-11-21T10:07:14.198
localDate2: 2019-11-21
localTime2: 10:07:14.198

4、Instant 使用示例

// 创建Instant对象
Instant instant = Instant.now();
// 获取秒数
long currentSecond = instant.getEpochSecond();
System.out.println(currentSecond);
// 获取毫秒数
long currentMilli = instant.toEpochMilli();
System.out.println(currentMilli);

运行结果

1574302070
1574302070013

5、 使用示例

LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
System.out.println("localDateTime: " + localDateTime);
// 增加一年
localDateTime = localDateTime.plusYears(1);
System.out.println("localDateTime: " + localDateTime);

localDateTime = localDateTime.plus(1, ChronoUnit.YEARS);
System.out.println("localDateTime: " + localDateTime);

// 减少一个月
localDateTime = localDateTime.minusMonths(1);
System.out.println("localDateTime: " + localDateTime);

localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS);
System.out.println("localDateTime: " + localDateTime);

// 修改年为2019
localDateTime = localDateTime.withYear(2020);
System.out.println("localDateTime: " + localDateTime);

// 修改为2022
localDateTime = localDateTime.with(ChronoField.YEAR, 2022);
System.out.println("localDateTime: " + localDateTime);

运行结果

localDateTime: 2019-09-10T14:46:56
localDateTime: 2020-09-10T14:46:56
localDateTime: 2021-09-10T14:46:56
localDateTime: 2021-08-10T14:46:56
localDateTime: 2021-07-10T14:46:56
localDateTime: 2020-07-10T14:46:56
localDateTime: 2022-07-10T14:46:56

6、结合spring-boot使用

    // @JsonSerialize(using = DateConverter.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

转换类

package com.hao.util;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class LocalDateTimeConverter extends JsonSerializer<LocalDateTime> {

    @Override
    public void serialize(LocalDateTime ldt, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeNumber(ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli());
    }
}

总结:
1、DateTimeFormatter默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter的ofPattern方法创建自定义格式化方式
2、和SimpleDateFormat相比,DateTimeFormatter是线程安全的
3、Date有的方法,LocalDateTime都有,而且更全面,更休息,推荐使用

posted on 2019-11-21 10:16  牛鼻子老赵  阅读(269)  评论(0编辑  收藏  举报