日期相关类详细解析

日期相关类详细解析

Java8之前的日期相关类(熟悉)

System类的概述

  1. 基本概念

    Java.lang.System类中提供了一些有用的类字段和方法

  2. 常用的方法

    方法声明功能介绍
    static long currentTimeMillis()返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时 间差
  3. 注意事项

    通常用于测试某一段代码的执行效率

Date类的概述

  1. 基本概念

    java.util.Date类主要用于描述特定的瞬间,也就是年月日时分秒,可以精确到毫秒。

  2. 常用的方法

    方法声明功能介绍
    Date()使用无参的方式构造对象,也就是当前系统时间
    Date(long date)根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒 的毫秒数
    long getTime()获取调用对象距离1970年1月1日0时0分0秒的毫秒数
    void setTime(long time)设置调用对象为距离基准时间time毫秒的时间点
  3. 代码演示

      public static void main(String[] args) {
    
            // 1.使用无参方式构造Date对象并打印
            Date d1 = new Date();
            System.out.println("d1 = " + d1); // 获取当前系统时间
    
            // 2.使用参数指定的毫秒数来构造Date对象并打印  1秒 = 1000毫秒  东八区
            Date d2 = new Date(1000);
            System.out.println("d2 = " + d2); // 1970 1 1 8 0 1
    
            // 3.获取调用对象距离1970年1月1日0时0分0秒的毫秒数
            long msec = d2.getTime();
            System.out.println("获取到的毫秒数是:" + msec); // 1000
    
            // 4.设置调用对象所表示的时间点为参数指定的毫秒数
            d2.setTime(2000);
            System.out.println("修改后的时间是:" + d2); // 1970 1 1 8 0 2
        }
    

SimpleDateFormat类的概述

  1. 基本概念

    java.text.SimpleDateFormat类主要用于实现日期和文本之间格式的转换

  2. 常用的方法

    方法声明功能介绍
    SimpleDateFormat()使用无参方式构造对象
    SimpleDateFormat(String pattern)根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒
    final String format(Date date)用于将日期类型转换为文本类型
    Date parse(String source)用于将文本类型转换为日期类型
  3. 代码演示

     public static void main(String[] args) throws ParseException {
            // 1.获取当前系统时间
            Date date = new Date();
            // 2.构造SimpleDateFormat类型的对象
            SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            // 3.实现日期类转换为文本类型并打印
            String format1 = format.format(date);
            System.out.println(format1);
            // 4.实现文本类型转换为日期类型
            Date parse = format.parse(format1);
            System.out.println(parse);
        }
    

Calendar类的概述

  1. 基本概念

    • java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化
    • 该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的 是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。
  2. 常用的方法

    方法声明功能介绍
    static Calendar getInstance()用于获取Calendar类型的引 用
    void set(int year, int month, int date, int hourOfDay, int minute, int second)用于设置年月日时分秒信息
    Date getTime()用于将Calendar类型转换为 Date类型
    void set(int field, int value)设置指定字段的数值
    void add(int field, int amount)向指定字段增加数值
  3. 代码演示

    	 	// 5.获取Calender的实例化对象
            Calendar instance = Calendar.getInstance();
            // 5.1设置时间
            instance.set(2008,8-1, 8, 20, 8, 8);
            // 5.2转化为为Date对象
            Date time = instance.getTime();
    		// 格式转换
            String format2 = format.format(time);
            System.out.println(format2); // 2008年08月08日 20:08:08
    
  4. 多态的使用场合

    • 通过方法的参数传递形成多态;

      public static void draw(Shape s){ s.show(); }

      draw(new Rect(1, 2, 3, 4));

    • 在方法体中直接使用多态的语法格式

      Account acc = new FixedAccount();

    • 通过方法的返回值类型形成多态

      Calender getInstance(){ return new GregorianCalendar(zone, aLocale); }

Java8中的日期相关类(熟悉)

Java8日期类的由来

  • JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被 弃用 了。而Calendar并不比Date好多少。它们面临的问题是:
    • Date类中的年份是从1900开始的,而月份都从0开始
    • 格式化只对Date类有用,对Calendar类则不能使用。
    • 非线程安全等。

Java8日期类的概述

  • Java 8通过发布新的Date-Time API来进一步加强对 日期与时间的处理。
  • java.time包:该包日期/时间API的基础包。
  • java.time.chrono包:该包提供对不同日历系统的访问。
  • java.time.format包:该包能够格式化和解析日期时间对象。
  • java.time.temporal包:该包包含底层框架和扩展特性。
  • java.time.zone包:该包支持不同时区以及相关规则的类。

LocalDate类的概述

  1. 基本概念

    java.time.LocalDate 类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。

  2. 常用的方法

    方法声明功能介绍
    static LocalDate now()在默认时区中从系统时钟获取当前日期

LocalTime类的概述

  1. 基本概念

    java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。

  2. 常用的方法

    方法声明功能介绍
    static LocalDate now()从默认时区的系统时间中获取当前时间
    static LocalTime now(ZoneId zone)获取指定时区的当前时间

LocalDateTime类的概述

  1. 基本概念

    java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12- 03T10:15:30。

  2. 常用的方法

    方法声明功能介绍
    static LocalDateTime now()从默认时区的系统时间中获取 当前日期时间
    static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)根据参数指定的年月日时分秒 信息来设置日期时间
    int getYear()获取年份字段的数值
    int getMonthValue()获取1到12之间的月份字段
    int getDayOfMonth()获取日期字段
    int getHour()获取小时数
    int getMinute()获取分钟数
    int getSecond()获取秒数
    LocalDateTime withYear(int year)设置为参数指定的年
    LocalDateTime withMonth(int month)设置为参数指定的月
    LocalDateTime withDayOfMonth(int dayOfMonth)设置为参数指定的日
    LocalDateTime withHour(int hour)设置为参数指定的时
    LocalDateTime withMinute(int minute)设置为参数指定的分
    LocalDateTime withSecond(int second)设置为参数指定的秒
    LocalDateTime plusYears(long years)加上参数指定的年
    LocalDateTime plusMonths(long months)加上参数指定的月
    LocalDateTime plusDays(long days)加上参数指定的日
    LocalDateTime plusHours(long hours)加上参数指定的时
    LocalDateTime plusMinutes(long minutes)加上参数指定的分
    LocalDateTime plusSeconds(long seconds)加上参数指定的秒
    LocalDateTime minusYears(long years)减去参数指定的年
    LocalDateTime minusMonths(long months)减去参数指定的月
    LocalDateTime minusDays(long days)减去参数指定的日
    LocalDateTime minusHours(long hours)减去参数指定的时
    LocalDateTime minusMinutes(long minutes)减去参数指定的分
    LocalDateTime minusSeconds(long seconds)减去参数指定的秒
  3. 方法使用

    1. 使用of方法将时间设置为2008年8月8日 20点08分08秒并按格式打印

       	   // 使用有参构造设置时间为 2008年8月8日 20点08分08秒 并打印
              LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);
              // 使用DateTimeFormatter 类进行时间的格式化处理
              System.out.println("未处理进行格式处理的时间是: " + of);
              DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
              String format1 = of.format(format);
              System.out.println("格式化处理后的时间是: " + format1);
      
    2. 使用get对应方法获取年月日 时分秒 2008年08月08日 20:08:08 的数据并分别打印

              System.out.println("指定时间的年份是" + of.getYear()); // 获取年的字段 返回值为 int
              System.out.println("指定时间的年份是" + of.getMonthValue()); // 获取月的字段 返回值为 int
              System.out.println("指定时间的年份是" + of.getDayOfMonth()); // 获取天的字段 返回值为 int
              System.out.println("指定时间的年份是" + of.getHour()); // 获取小时的字段 返回值为 int
              System.out.println("指定时间的年份是" + of.getMinute()); // 获取分钟的字段 返回值为 int
              System.out.println("指定时间的年份是" + of.getSecond()); // 获取秒的字段 返回值为 int
      
    3. 使用with对应方法设置 年月日信息 2020年 10月 10日 并按格式输出

             // 使用with对应方法设置 年月日信息 2020年 10月 10日 并按格式输出 注意有效范围 和日期时间规定相同
              System.out.println("设置前的时间是 " + of.format(format)); // 2008年08月08日 20:08:08 注意LocalDateTime.format() 传入的参数是  DateTimeFormatter
              of = of.withYear(2020); // 设置年信息
              of = of.withMonth(10); // 设置月信息
              of = of.withDayOfMonth(10); // 设置日信息
              System.out.println("设置后的时间是 " + of.format(format)); // 2020年10月10日 20:08:08
      
    4. 使用plug对应方法 将时分秒进行加 30 并打印

              // 使用plug对应方法  将时分秒进行加 30 并打印 进行相加是 如果超过对应规则 会自动进位处理
              System.out.println("加之前的时间是 " + of.format(format)); // 2020年10月10日 20:08:08
              of = of.plusHours(30);  // 小时加20
              of = of.plusMinutes(30); // 分钟加20
              of = of.plusSeconds(30); // 秒数加20
              System.out.println("加之后的时间是 " + of.format(format)); // 2020年10月12日 02:38:38
      
    5. 使用minus对应方法 年月日减去 10 并打印

              // 使用minus对应方法 年月日减去 10 并打印  进行相减是 如果超过对应规则 会自动借位处理
              System.out.println("减之前的时间是 " + of.format(format));  // 2020年10月12日 02:38:38
              of = of.minusYears(10); // 年份减10
              of = of.minusMonths(10); // 月份减10
              of = of.minusDays(10); // 日减10
              System.out.println("减之后的时间是 " + of.format(format)); // 2009年12月02日 02:38:38
      

Instant类的概述

  1. 基本概念

    java.time.Instant类主要用于描述瞬间的时间点信息

  2. 常用的方法

    方法声明功能介绍
    static Instant now()从系统时钟上获取当前时间
    OffsetDateTime atOffset(ZoneOffset offset)将此瞬间与偏移量组合以创建偏移日期时间
    static Instant ofEpochMilli(long epochMilli)根据参数指定的毫秒数来构造对象,参数为距离1970年1月1 日0时0分0秒的毫秒数
    long toEpochMilli()获取距离1970年1月1日0时0分0秒的毫秒数
  3. 方法的使用

            // 1.获取Instant当前系统时间 并不是当前系统的默认时区  获取到的是本初子午线 差8小时
            Instant instant = Instant.now();
            System.out.println("获取到的系统时间是:" + instant); // 2021-08-13T15:24:22.526338600Z
    
            // 2.加上时区上所差的8小时
            OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
            System.out.println("加上时区差的时间是:" + offsetDateTime); // 2021-08-13T23:24:22.526338600+08:00
    
            // 3.获取当前系统时间距离1970年1月1 日0时0分0秒的毫秒数
            long g = instant.toEpochMilli();
            System.out.println("获取到的毫秒数是 " + g); // 1628868570830
    
            // 4.根据参数指定的好面熟来构造对象
            Instant milli = Instant.ofEpochMilli(g);
            System.out.println("根据毫秒数来构造的对象是:" + milli); // 2021-08-13T15:29:30.830Z
    
    

DateTimeFormatter类的概述

  1. 基本概念

    java.time.format.DateTimeFormatter类主要用于格式化和解析日期。

  2. 常用的方法

    方法声明功能介绍
    static DateTimeFormatter ofPattern(String pattern)根据参数指定的模式来获取对象
    String format(TemporalAccessor temporal)将参数指定日期时间转换为字符串
    TemporalAccessor parse(CharSequence text)将参数指定字符串转换为日期时间
  3. 方法的使用

            // 1.获取当前系统的日期时间并打印
            LocalDateTime now = LocalDateTime.now();
            System.out.println("当前的系统时间是 " + now); // 2021-08-13T23:36:28.647157900
            // 2.按照指定的格式准备一个DateTimeFormatter类型的对象
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
            // 3.实现日期时间向字符串类型的转换并打印
            String str = dateTimeFormatter.format(now);
            System.out.println("调整格式后的结果是:" + str); // 2021年08月13日 23:37:05
            // 4.实现字符串类型到日期时间类型的转换并打印
            TemporalAccessor parse = dateTimeFormatter.parse(str);
            System.out.println("转回去的结果是:" + parse); // {},ISO resolved to 2021-08-13T23:37:05
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白迷上java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值