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

36 阅读4分钟
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);
    }
}

🍀 打印出来的时间⏰非常国际范(不符合中国人的阅读习惯😀)
🍀 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);
    }
}

🍀 上面的代码:传入【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));
    }
}

二、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
    }
}

三、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;
        }
    }
}

在这里插入图片描述

四、Calendar

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

public class TestDemo {
    public static void main(String[] args) {
        // Calendar 的实例中包含了当前时间的信息
        Calendar calendar = Calendar.getInstance();



![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/8a6702924e604ddc8cf98a507fb4015e~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1770647605&x-signature=b8FEYRnBvG0gJwrywaucBKG9%2F8M%3D)
![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/715b068358f34c31b6fa49c0ccabc51d~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1770647605&x-signature=XxYsfjC%2Fyo2V003x8KAnD5t5HfU%3D)
![img](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/6cd7ccdc11b648459d4af4cfd44c2bf3~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg5py65Zmo5a2m5Lmg5LmL5b-DQUk=:q75.awebp?rk3s=f64ab15b&x-expires=1770647605&x-signature=SM3Z8Iu8dBGVYtGGlKL6DGXbFKY%3D)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**


**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://gitee.com/vip204888)**