• Java8新增日期类LocalDateTime,LocalDateTime与Date相比有什么优势


    JDK8之前Date类

    1.Date类概述和构造方法

    2.Date的常用方法

    public class DateDemo2 {
        public static void main(String[] args) {
            Date date1 = new Date();
            date1.setTime(0L);
            System.out.println(date1);
            //methods1();
        }
    
        public static void methods1() {
            //getTime():获取当前时间
            Date date1 = new Date();
            System.out.println(date1.getTime());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    JDK8之前SimpleDateFormat类概述

    SimpleDateFormat可以对Date对象,进行格式化和解析

    1.SimpleDateFormat的构造方法

    2.SimpleDateFormat格式化和解析日期

    ① 格式化(从Date到String)

    • public String format(Date date):将日期格式化日期/时间字符串,把时间按照固定格式展示

    ② 解析(从String到Date)

    • pubic Date parse(String source):从给定字符串的开始解析文本以生成文本,需要对时间进行计算

    格式化

    public class DateDemo3 {
        public static void main(String[] args) {
            Date date = new Date();
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH时mm分ss秒");
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd");
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            System.out.println(format);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解析

    public class DateDemo4 {
        public static void main(String[] args) throws ParseException {
            String s = "2022-11-13";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(s);
            System.out.println(date);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    JDK8新增日期类

    1.JDK8Date分类

    2.LocalDateTime创建时间

    public class JDK8DateDemo2 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime now = LocalDateTime.now();
            System.out.println(now);
            LocalDateTime time = LocalDateTime.of(2020, 11, 1, 8, 10, 10);
            DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String format = time.format(dft);
            System.out.println(format);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.LocalDateTime获取方法

    public class JDK8DateDemo3 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
            //public int getYear()
            int timeYear = dateTime.getYear();
            System.out.println("年为 " + timeYear);
            //public int getMonthValue()
            int monthValue = dateTime.getMonthValue();
            System.out.println("月为 " + monthValue);
            //public int getDayOfMonth()
            int dayOfMonth = dateTime.getDayOfMonth();
            System.out.println("月中第 " + dayOfMonth + "天");
            //public int getDayOfYear()
            int dayOfYear = dateTime.getDayOfYear();
            System.out.println("年中第 " + dayOfYear + "天");
            //public DayOfWeek getDayOfWeek()
            DayOfWeek dayOfWeek = dateTime.getDayOfWeek();
            System.out.println("星期为 " + dayOfWeek);
            //public int getHour()
            int hour = dateTime.getHour();
            System.out.println("小时为 " + hour);
            //public int getMinute()
            int minute = dateTime.getMinute();
            System.out.println("分钟为 " + minute);
            //public int getSecond()
            int second = dateTime.getSecond();
            System.out.println("秒为 " + second);
        }
    }
    
    • 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

    4.LocalDateTime转换方法

    public class JDK8DateDemo4 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
            LocalDate localDate = dateTime.toLocalDate();
            System.out.println(localDate);
            LocalTime localTime = dateTime.toLocalTime();
            System.out.println(localTime);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    5.LocalDateTime格式化和解析

    6.JDK8的日期格式化器:DateTimeFormatter

    public class JDK8DateDemo5 {
        public static void main(String[] args) throws ParseException {
           method2();
           // method1();
        }
    
        public static void method2() {
            String s ="2008年08月07日 13:14:15";
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            LocalDateTime date= LocalDateTime.parse(s, pattern);
            System.out.println(date);
        }
    
        public static void method1() {
            LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
            System.out.println(dateTime);
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String date = dateTime.format(pattern);
            System.out.println(date);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    7.LocalDateTime增加或者减少时间的方法

    • 方法返回一个新的LocalDateTime对象,返回值就是修改之后的结果

    • 参数为正,就是往后加

    • 参数为负,就是往前减

    public class JDK8DateDemo6 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
            LocalDateTime nweDateTime = dateTime.plusYears(1);
            System.out.println(nweDateTime);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8.LocalDateTime修改方法

    public class JDK8DateDemo7 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime dateTime = LocalDateTime.of(2020, 11, 12, 8, 10, 10);
            LocalDateTime newDate = dateTime.withYear(2066);
            System.out.println(newDate);
            LocalDateTime dateTime1 = dateTime.withMonth(10);
            System.out.println(dateTime1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Period类

    public class JDK8DateDemo8 {
        public static void main(String[] args) throws ParseException {
            LocalDate dateTime1 = LocalDate.of(2020, 11, 12);
            LocalDate dateTime2 = LocalDate.of(2028, 10, 10);
            //public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
            Period period = Period.between(dateTime1, dateTime2);
            System.out.println(period);//P7Y10M28D
            //public int getYears() //这段时间的年数
            System.out.println(period.getYears());//7
    
            //public int getMonths()//这段时间的月数
            System.out.println(period.getMonths());//10
            //public int getDays()//这段时间的天数
            System.out.println(period.getDays());//28
            //  public long toTotalMonths()//这段时间的总月数
            System.out.println(period.toTotalMonths());//94
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Duration

    public class JDK8DateDemo9 {
        public static void main(String[] args) throws ParseException {
            LocalDateTime localDateTime1 = LocalDateTime.of(2020, 11, 12, 10, 13, 14);
            LocalDateTime localDateTime2 = LocalDateTime.of(2020, 11, 13, 12, 10, 15);
    
            Duration duration = Duration.between(localDateTime1, localDateTime2);
            System.out.println(duration);//PT25H57M1S
            // public long toHours(): 时间间隔的小时
            System.out.println(duration.toHours());//25
    
            // public long toMinutes(): 时间间隔的分钟
            System.out.println(duration.toMinutes());//1557
    
            //public long toSeconds(): 时间间隔的秒
            System.out.println(duration.toSeconds());93421
    
            //public long toMillis():时间间隔的毫秒
            System.out.println(duration.toMillis());//93421000
    
            //public long toNanos():时间间隔的纳秒
            System.out.println(duration.toNanos());//93421000
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    【跨境电商】全渠道客户服务终极指南(一):概念,重要性与优势
    20230830比赛总结
    怎样提高服务器安全性?45.248.10.x
    什么是FutureTask ?
    解决问题遇到的问题
    Docker网络学习
    农村人口房屋管理系统(VB+access)
    机器学习笔记之高斯网络(二)高斯贝叶斯网络
    如何守护数据安全? 这里有一份RDS灾备方案为你支招
    京东按关键字搜索商品 API
  • 原文地址:https://blog.csdn.net/weixin_46665411/article/details/127837300