• JAVA基础(四十一)——常用类之日期类


    一、目录

    • Date类
    • Calendar类
    • Local相关日期类

    二、Date类

    第一代日期类是Date,并且配合SimpleDateFormat类使用。

    1. Date:精确到毫秒,代表特定的瞬间。

      new Date();这个无参构造器就是获取当前时间的,不过是按照某种格式显示的。这种格式不太方便使用,一般会对格式进行转换。

    2. SimpleDateFormat:格式和解析日期的类。SimpleDateFormat格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

    创建SimpleDateFormat对象,指定相应的格式,但是格式使用的字母是规定好的,不能乱写。

    在这里插入图片描述

    获取时间

    • new date()直接获取。
    • new date(xxxx)通过毫秒数来获取。
    • simpleDateFormat类实例的parse方法解析格式化的时间字符串来获取。
    package com.javadate;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class TestDate {
        public static void main(String[] args) throws ParseException {
             //1. 直接获取当前时间
            Date date = new Date();
            System.out.println(date);
    
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
            String format = simpleDateFormat.format(date);
            System.out.println(format);
    
            //2. 还可以通过毫秒数获取时间
            Date date1 = new Date(9234567);  
            System.out.println(date1);
    
            //3. 还可以把格式化的时间字符串,转换成对应的Date
            Date parse = simpleDateFormat.parse("2012年01月25日 11:26:11 星期三");
            System.out.println(parse);
        }
    }
    
    Thu Sep 15 01:20:15 CST 2022
    2022091501:20:15 星期四
    Thu Jan 01 10:33:54 CST 1970
    Wed Jan 25 11:26:11 CST 2012
    
    • 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

    三、Calendar类

    Calendar日历类是第二代日期类。

    Calendar类是一个抽象类,它为特定瞬间与一组诸如YEAR、MONTH、DAY_OF_MONTH、HOUR等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

    1. Calendar是一个抽象类,并且构造器是protected。
    2. 可以通过getInstance()来获取实例。
    3. 提供大量的方法和字段给程序员。
    4. Calendar没有专门的格式化方法,需要程序员自己来组合输出显示。
    5. 如果需要24小时来获取时间,Calendar.HOUR改成Calendar.HOUR_OF_DAY就行。
    package com.javadate;
    
    import java.util.Calendar;
    
    public class TestCalendar {
        public static void main(String[] args) {
            Calendar c = Calendar.getInstance();
            System.out.println(c);
    
            //获取日历对象的某个日历字段
    
            System.out.println("Year: " + c.get(Calendar.YEAR));
            System.out.println("Month: " + c.get(Calendar.MONTH + 1)); //返回月时,是从0开始编号的
            System.out.println("Day: " + c.get(Calendar.DAY_OF_MONTH));
            System.out.println("Hour: " + c.get(Calendar.HOUR));
            System.out.println("Minute: " + c.get(Calendar.MINUTE));
            System.out.println("Second: " + c.get(Calendar.SECOND));
        }
    }
    
    
    Year: 2022
    Month: 38
    Day: 15
    Hour: 2
    Minute: 9
    Second: 38
    
    • 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

    四、Local相关日期类

    前面两代日期类不足的分析:
    JDK1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK1.1引入Calendar类之后就被弃用了。
    而Calendar类也存在一些问题:

    1. 可变性:像日期和时间这样的类应该是不可变的。
    2. 偏移性:Date中的年份是从1970开始的,而月份都是从0开始的。
    3. 格式化:格式化只对Date有用,而Calendar则不行。
    4. 此外,她们也不是线程安全的,不能处理闰秒等(每隔两天,多出1s)。

    针对上面这些不足,在jdk8中,引入了第三代日期类。
    第三代日期类包含:LocalDate(日期)、LocalTime(时间)、LocalDateTime(日期时间)。结合DateTimeFormatter格式日期类效果会更好。

    package com.javadate;
    
    import java.time.LocalDateTime;
    
    public class TestLocal {
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            System.out.println(now);
            System.out.println(now.getDayOfYear());
            System.out.println(now.getYear());
            System.out.println(now.getMonth());
            System.out.println(now.getMonthValue());
            System.out.println(now.getDayOfMonth());
            System.out.println(now.getHour());
            System.out.println(now.getMinute());
            System.out.println(now.getSecond());
    
    
        }
    }
    
    2022-09-15T02:22:31.154
    258
    2022
    SEPTEMBER
    9
    15
    2
    22
    31
    
    • 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

    DateTimeFormatter格式日期类类似于SimpleDateFormat。

    package com.javadate;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class TestLocal {
        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            System.out.println(now);
    
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss E");
            String format = dateTimeFormatter.format(now);
            System.out.println(format);
            
        }
    }
    
    
    2022-09-15T02:27:58.880
    2022091502:27:58 星期四
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Instant时间戳,类似于Date,提供了一系列和Date类转换的方式。

    package com.javadate;
    
    import java.time.Instant;
    import java.util.Date;
    
    public class TestInstance {
        public static void main(String[] args) {
            Instant now = Instant.now();  //返回当前时间戳
            System.out.println(now);
    
            Date from = Date.from(now);
            System.out.println(from);
    
            Instant instant = from.toInstant();  //通过toInstant将date的时间再重新转换成时间戳
            System.out.println(instant);
        }
    }
    
    
    2022-09-14T18:31:39.195Z
    Thu Sep 15 02:31:39 CST 2022
    2022-09-14T18:31:39.195Z
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    人工智能如何提高转录效率
    mmclassification安装与调试
    [实践篇]13.6 QNX侧如何抓取日志?
    如何使用windbg查看C#某个线程的栈大小 ?
    java-net-php-python-ssm仓库管理系统计算机毕业设计程序
    03excel函数2
    论如何直接用EF Core实现创建更新时间、用户审计,自动化乐观并发、软删除和树形查询(下)
    “闭关修炼”这么久,吃透这些“微服务”笔记,足够面试涨10K
    Ubuntu系统安装MySQL主从模式集群(成功!)
    使用Python求解方程
  • 原文地址:https://blog.csdn.net/qq_37466661/article/details/126863496