Java基础:JDK 8 之 LocalDate,LocalTime,LocalDateTime 使用

因为 Date 实在不方便,特别是在日期计算时难用,并且格式化是非线程安全的。

JDK 8 提供 LocalDate, LocalTime, LocalDateTime 可以更方便操作日期时间,是线程安全的。

在 JDK 8 出来之前,大多会使用第三方库 Joda 来操作日期时间,GitHub joda-time

国内工具类 Hutool 也提供了日期时间工具类,可以方便操作。

LocalDate

  1. LocalDate,LocalTime,LocalDateTime

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    import java.time.*;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.ChronoField;
    import java.time.temporal.ChronoUnit;

    public class LocalDateDemo {

    public static void main(String[] args) {

    //2019-10-30
    LocalDate localDate = LocalDate.now();
    //2019-09-10
    LocalDate localDate1 = LocalDate.of(2019, 9, 10);
    //2019-10-30T19:58:29.519
    LocalDateTime localDateTime = LocalDateTime.now();
    //19:58:29.520
    LocalTime localTime = LocalTime.now();

    //获取年:2019
    int year = localDate.getYear();
    //获取年:2019
    int year1 = localDate.get(ChronoField.YEAR);
    //获取月:OCTOBER
    Month month = localDate.getMonth();
    //获取月:10
    int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
    //获取天:30
    int day = localDate.getDayOfMonth();
    //获取天:30
    int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
    //获取星期:WEDNESDAY
    DayOfWeek dayOfWeek = localDate.getDayOfWeek();
    int dayOfMonth = localDate.getDayOfMonth();
    int dayOfYear = localDate.getDayOfYear();
    //获取星期:3
    int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);

    /**
    * LocalDateTime
    * 获取年月日时分秒,等于LocalDate+LocalTime
    */
    //2019-10-30T20:01:30.666
    LocalDateTime localDateTime0 = LocalDateTime.now();
    //2019-09-10T14:46:56
    LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
    //2019-10-30T20:01:30.665
    LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
    //2019-10-30T20:01:30.665
    LocalDateTime localDateTime3 = localDate.atTime(localTime);
    //2019-10-30T20:01:30.665
    LocalDateTime localDateTime4 = localTime.atDate(localDate);

    /**
    * 获取LocalDate
    */
    //2019-10-30
    LocalDate localDate2 = localDateTime.toLocalDate();

    /**
    * 获取LocalTime
    */
    //20:03:45.084
    LocalTime localTime2 = localDateTime.toLocalTime();

    /**
    * 获取秒数
    */
    Instant instant = Instant.now();
    //秒数:1572437086
    long currentSecond = instant.getEpochSecond();
    //毫秒:1572437086198
    long currentMilli = instant.toEpochMilli();
    long nowMilli = System.currentTimeMillis();//更方便

    /**
    * 日期计算
    */
    //2019-09-10T14:46:56
    LocalDateTime localDateTime11 = LocalDateTime.of(2019, Month.SEPTEMBER, 10,
    14, 46, 56);
    //增加一年:2020-09-10T14:46:56
    LocalDateTime localDateTime12 = localDateTime11.plusYears(1);
    //增加一年:2020-09-10T14:46:56
    LocalDateTime localDateTime13 = localDateTime11.plus(1, ChronoUnit.YEARS);
    //减少一个月:2019-08-10T14:46:56
    LocalDateTime localDateTime14 = localDateTime11.minusMonths(1);
    //减少一个月:2019-08-10T14:46:56
    LocalDateTime localDateTime15 = localDateTime11.minus(1, ChronoUnit.MONTHS);

    /**
    * 通过With修改值
    */
    //修改年2020:2020-10-30T20:11:54.369
    LocalDateTime localDateTime16 = localDateTime.withYear(2020);
    //修改为2022:2022-10-30T20:11:54.369
    LocalDateTime localDateTime17 = localDateTime.with(ChronoField.YEAR, 2022);

    /**
    * 格式化时间
    */
    //2019-09-10
    LocalDate localDate11 = LocalDate.of(2019, 9, 10);
    //20191030
    String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
    //2019-10-30
    String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
    //自定义格式化
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    //30/10/2019
    String s3 = localDate.format(dateTimeFormatter);

    //解析时间
    //2019-09-10
    LocalDate localDate12 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE);
    //2019-09-10
    LocalDate localDate13= LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE);

    }
    }

入参与出参

这种方式在入参时,根据需要格式添加日期时间格式化注解(@DateTimeFormat),灵活。

1
2
3
4
5
@JsonDeserialize(using = LocalDateTimeDeserializer.class)  //application/json
@JsonSerialize(using = LocalDateTimeSerializer.class) //application/json
@DateTimeFormat(pattern = "yyyy-MM-dd")//入参转换,对 form 或 url 传进来的起效
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy/MM/dd")//输出转换
private LocalDate birthday;

但如果能统一日期或时间的入参格式,且不想在每个属性上添加日期格式化注解,则可以自定义转换器,并注册为 Bean,就可自动将日期或时间格式化传入,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Configuration
public class LocalDateTimeConverter {

@Bean
public Converter<String, LocalDate> localDateConverter() {
return new Converter<String, LocalDate>() {
@Override
public LocalDate convert(String source) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
};
}

@Bean
public Converter<String, LocalDateTime> localDateTimeConverter() {
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String source) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
};
}
}

其它参考

  1. SpringMvc Json LocalDateTime 互转,form urlencoded @ModelAttribute 转换

Java基础:JDK 8 之 LocalDate,LocalTime,LocalDateTime 使用

http://blog.gxitsky.com/2019/10/30/Java-jdk-10-localdate/

作者

光星

发布于

2019-10-30

更新于

2022-06-17

许可协议

评论