• 37、一篇文章学习 Java 中的日期相关类(Date 和 Calendar),非常常用


    一、认识 Date

    📝 java.util.Date 是开发中常用的日期处理

    🍀 日期处理在开发中很常用。一般来说,每一张数据库表都应该存在一个 create_time 字段,用于表示这张表的某条数据的插入时间
    🍀 若用 MySQL 的 CURRENT_TIMESTAMP(当前时间戳)的话,当遇到低版本的 MySQL 的话就会报错
    🍀 所以,可通过 Java 的 java.util.Date 类给表的 create_time 字段提供值

    📝 一个没有给构造方法传参数的 Date 对象表示的是创建该 Date 对象时的时间⏰(包括:年、月、日、时、分、秒、星期 …)

    public class TestDemo {
        public static void main(String[] args) {
            Date date = new Date();
            // output: Mon Oct 24 03:00:48 CST 2022
            System.out.println(date);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    🍀 打印出来的时间⏰非常国际范(不符合中国人的阅读习惯😀)
    🍀 Mon:表示星期一
    🍀 Oct:表示十月份
    🍀 CST:China Standard Time(中国标准时间)

    📝 给 Date 类的构造方法传一个 long 类型的参数的话,表示的是:格林尼治时间的1970年1月1日0时0分0秒加上参数毫秒后的时间
    在这里插入图片描述

    public class TestDemo {
        public static void main(String[] args) {
            Date date = new Date(1000);
            // Thu Jan 01 08:00:01 CST 1970
            System.out.println(date);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    🍀 上面的代码:传入【1000】作为 Date 类的参数,表示的是格林尼治时间的1970年1月1日0时0分0秒1000毫秒后的时间
    🍀 1000毫秒等于1秒,所以是:Thu Jan 01 08:00:01 CST 1970
    🍀 打印出来的时间是08,因为中国比格林尼治时间快八个小时(时区)

    📝 System.currentTimeMillis()返回的是从格林尼治时间的1970年1月1日0时0分0秒到此时此刻所经历的毫秒数(即时间戳)

    public class TestDemo {
        public static void main(String[] args) {
            Date d1 = new Date(System.currentTimeMillis());
            Date d2 = new Date();
            // true: d1 和 d2 表示的是同一个时间
            System.out.println(d1.equals(d2));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    二、Date 常用方法

    public class TestDemo {
        public static void main(String[] args) {
            Date d1 = new Date(System.currentTimeMillis());
            Date d2 = new Date();
    
            // 设置毫秒数
            d1.setTime(1000);
            d2.setTime(2000);
    
            // 获取毫秒数
            long d1Mills = d1.getTime();
            long d2Mills = d2.getTime();
    
            // 比较时间
            boolean ret1 = d2.after(d1); // output: true
            boolean ret2 = d1.before(d2); // output: true
            int ret3 = d1.compareTo(d2); // output: -1
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    三、SimpleDateFormat

    📝 java.text.SimpleDateFormat常用来做日期的格式化处理(重要)

    public class TestDemo {
        public static void main(String[] args) throws ParseException {
            String s = "2011-08-13 19:35:38";
            String e = "2023-08-13 19:35:38";
    
            System.out.println(GqDates.inThisTimeRange(s, e));
    
        }
    }
    
    class GqDates {
        public static final String YMDHMS_24_DATE_STR = "yyy-MM-dd HH:mm:ss";
        public static final String YMDHMS_12_DATE_STR = "yyy-MM-dd hh:mm:ss";
        public static final String MILL_DATE_STR = "yyyyMMddHHmmssS";
    
        /**
         * 生成毫秒时间字符串(如 20221024034702656)
         * 该字符串可简单看做一个【唯一字符串】
         */
        public static String curMillTimeStr() {
            return createCurTimeString(MILL_DATE_STR);
        }
    
        /**
         * 生成时间字符串(二十四小时制)
         */
        public static String curTimeStr24() {
            return createCurTimeString(YMDHMS_24_DATE_STR);
        }
    
        /**
         * 生成时间字符串(十二小时制)
         */
        public static String curTimeStr12() {
            return createCurTimeString(YMDHMS_12_DATE_STR);
        }
    
        /**
         * 生成指定格式的, 当前时间的字符串
         *
         * @param pattern 日期格式
         */
        public static String createCurTimeString(String pattern) {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(new Date());
        }
    
        /**
         * 把指定格式的日期字符串转换为日期对象 (Date)
         *
         * @param str     日期字符串
         * @param pattern 日期格式
         * @return 日期对象
         */
        public static Date string2DateObj(String str, String pattern) throws ParseException {
            return new SimpleDateFormat(pattern).parse(str);
        }
    
        /**
         * 获取某个时间字符串的毫秒值(时间戳)
         * 时间戳查询网:http://shijianchuo.wiicha.com/
         */
        public static long getMills(String dateStr, String pattern) throws ParseException {
            Date date = string2DateObj(dateStr, pattern);
            return date.getTime();
        }
    
        /**
         * 判断当前时间是否在某段时间范围内
         */
        public static boolean inThisTimeRange(String start, String end) {
            try {
                long s = getMills(start, YMDHMS_24_DATE_STR);
                long e = getMills(end, YMDHMS_24_DATE_STR);
                long cur = System.currentTimeMillis();
                return s < cur && cur < e;
            } catch (ParseException e) {
                e.printStackTrace();
                return false;
            }
        }
    }
    
    • 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

    在这里插入图片描述

    四、Calendar

    在这里插入图片描述
    📝 java.util.Calendar 也是开发中经常使用的日期处理
    📝 它的功能更加丰富(Date 中很多过期的方法都迁移到了 Calendar 中)

    public class TestDemo {
        public static void main(String[] args) {
            // Calendar 的实例中包含了当前时间的信息
            Calendar calendar = Calendar.getInstance();
    
            // 当前年
            int year = calendar.get(Calendar.YEAR);
            // 当前月【取值范围[0, 11], 0是一月, 11是十二月
            int month = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            int mill = calendar.get(Calendar.MILLISECOND);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    五、Calendar 常用方法

    public class TestDemo {
        public static void main(String[] args) {
            Calendar c = Calendar.getInstance();
            // 2022 8 8
            c.set(2022, Calendar.SEPTEMBER, 8);
            // 2022 8 10
            c.add(Calendar.DAY_OF_MONTH, 2);
            // 2022 10 10
            c.add(Calendar.MONTH, 2);
    
            // 获取 Date 对象
            c.getTime();
    
            // 设置 Date 对象
            c.setTime(new Date());
    
            // 设置毫秒数
            c.setTimeInMillis(System.currentTimeMillis());
    
            // 获取毫秒数
            c.getTimeInMillis();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    LAMP及论坛搭建
    【21天学习挑战】经典算法之【插入排序】
    原来Python自带了数据库,用起来真方便
    org.apache.commons.lang3.StringUtils工具类使用大全
    江苏魔百盒M301H_Hi3798MV300-300H-310芯片通刷-免费卡刷固件包
    1688往微信小程序自营商城铺货商品采集API接口
    YOLOv5算法改进(7)— 添加单层注意力机制(包括代码+添加步骤+网络结构图)
    2022年最新青海水利水电施工安全员模拟试题题库及答案
    Dubbo SPI扩展机制源码详解(基于2.7.10)
    代码安全之代码混淆及加固(Android)
  • 原文地址:https://blog.csdn.net/m0_54189068/article/details/127484342