美文网首页
Date、LocalDate、LocalTime、LocalDa

Date、LocalDate、LocalTime、LocalDa

作者: 纯是不纯 | 来源:发表于2021-07-14 17:14 被阅读0次

    java8全新的日期和时间API

    LocalDate、LocalTime、LocalDateTime是java8对日期、时间提供的新接口。实际使用中,计算日期就用LocalDate,计算日期加时刻用LocalDateTime,如果只有时刻就是LocalTime

    LocalDate

    只会获取年月日
    创建LocalDate

    //获取当前年月日
    LocalDate localDate = LocalDate.now();
    //构造指定的年月日
    LocalDate localDate1 = LocalDate.of(2019, 9, 10);
    //获取年、月、日、星期几
    int year = localDate.getYear();
    int year1 = localDate.get(ChronoField.YEAR);
    Month month = localDate.getMonth();
    int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
    int day = localDate.getDayOfMonth();
    int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
    DayOfWeek dayOfWeek = localDate.getDayOfWeek();
    int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
    

    LocalTime

    只会获取几点几分几秒

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

    LocalDateTime

    获取年月日时分秒,等于LocalDate+LocalTime

    //创建LocalDateTime
    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56);
    LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
    LocalDateTime localDateTime3 = localDate.atTime(localTime);
    LocalDateTime localDateTime4 = localTime.atDate(localDate);
    //获取LocalDate
    LocalDate localDate2 = localDateTime.toLocalDate();
    //获取LocalTime
    LocalTime localTime2 = localDateTime.toLocalTime();
    

    区别

    Date如果不格式化,打印出的日期可读性差
    Tue Sep 10 09:34:04 CST 2019
    使用SimpleDateFormat对时间进行格式化,但SimpleDateFormat是线程不安全的。

    遇到的问题

    在使用fastJson中对Date时间输出时会把时间字段转换为时间戳,其他类型的时间不会转换

    package org.example.kafka.message.enties;
    
    import com.alibaba.fastjson.JSON;
    
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.LocalTime;
    import java.util.Date;
    
    public class DateTest {
        public static void main(String[] args) {
            Message message = new Message();
            message.setLocalDate(LocalDate.now());
            message.setTime(new Date());
            message.setName("date");
            message.setLocalDateTime(LocalDateTime.now());
            message.setLocalTime(LocalTime.now());
            System.out.println(JSON.toJSONString(message));
            //打印输出,把time转化为时间戳了
            //{"localDate":"2021-07-14","localDateTime":"2021-07-14T17:13:06.219",
            // "localTime":"17:13:06.219","name":"date","time":1626253986219}
        }
    
        static class Message {
            private String name;
            private Date time;
            private LocalDate localDate;
            private LocalDateTime localDateTime;
            private LocalTime localTime;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Date getTime() {
                return time;
            }
    
            public void setTime(Date time) {
                this.time = time;
            }
    
            public LocalDate getLocalDate() {
                return localDate;
            }
    
            public void setLocalDate(LocalDate localDate) {
                this.localDate = localDate;
            }
    
            public LocalDateTime getLocalDateTime() {
                return localDateTime;
            }
    
            public void setLocalDateTime(LocalDateTime localDateTime) {
                this.localDateTime = localDateTime;
            }
    
            public LocalTime getLocalTime() {
                return localTime;
            }
    
            public void setLocalTime(LocalTime localTime) {
                this.localTime = localTime;
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Date、LocalDate、LocalTime、LocalDa

          本文链接:https://www.haomeiwen.com/subject/xowypltx.html