日期格式转化

1 字母含义

y 表示年份 yyyy为四位数年份,如2023,yy为两位数年份,如23

M 表示月份 MM为两位数月份,如02,M为以为数月份,如2

d 表示日期   D表示一年中的第几天

h或H表示小时,h是12小时制,H是24小时制

m 表示分钟

s 表示秒

public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        // 2023-02-20 周一
        calendar.set(2023, Calendar.FEBRUARY, 20);
        Date date = calendar.getTime();

        /**
         * 年份格式,下方代码输出结果为:
         * yyyyMMdd:20230220
         * yyMMdd:230220
         */
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyMMdd");
        System.out.println("yyyyMMdd:" + df1.format(date));
        System.out.println("yyMMdd:" + df2.format(date));

        /**
         * 月份格式,下方代码输出结果为:
         * yyyyMMdd:20230220
         * yyyyMdd:2023220
         */
        SimpleDateFormat df3 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat df4 = new SimpleDateFormat("yyyyMdd");
        System.out.println("yyyyMMdd:" + df3.format(date));
        System.out.println("yyyyMdd:" + df4.format(date));

        /**
         * 小时格式,下方代码输出结果为:
         * yyyy-MM-dd hh-mm-ss:2023-02-20 11-10-58
         * yyyy-MM-dd HH-mm-ss:2023-02-20 23-10-58
         */
        SimpleDateFormat df5 = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        SimpleDateFormat df6 = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        System.out.println("yyyy-MM-dd hh-mm-ss:" + df5.format(date));
        System.out.println("yyyy-MM-dd HH-mm-ss:" + df6.format(date));
    }

2 YYYYMMdd和yyyyMMdd区别

YYYY表示当天所在周所属的年份。一周从周日开始,周六结束。如果本周跨年,那么这周算入下一年。

如:2021-12-29为星期三,当周六是2022-01-01,存在着跨年,如果用YYYYMMdd展示出来的结果为2022-12-29,而yyyyMMdd展示出来的结果为2021-12-29,所以一般线上都是用yyyyMMdd

public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        // 2021-12-25 周六,不跨年
        calendar.set(2021, Calendar.DECEMBER, 25);
        Date date1 = calendar.getTime();
        // 2021-12-29  周三,当周跨年
        calendar.set(2021, Calendar.DECEMBER, 29);
        Date date2 = calendar.getTime();
        // 2022-01-01 周六,当周跨年
        calendar.set(2022, Calendar.JANUARY, 1);
        Date date3 = calendar.getTime();

        DateFormat df1 = new SimpleDateFormat("yyyyMMdd");
        DateFormat df2 = new SimpleDateFormat("YYYYMMdd");

        // yyyyMMdd
        System.out.println("yyyyMMdd 2021-12-25: " + df1.format(date1));
        System.out.println("yyyyMMdd 2021-12-29: " + df1.format(date2));
        System.out.println("yyyyMMdd 2022-01-01: " + df1.format(date3));
        System.out.println("=============================");

        // YYYYMMdd
        System.out.println("YYYYMMdd 2021-12-25: " + df2.format(date1));
        System.out.println("YYYYMMdd 2021-12-29: " + df2.format(date2));
        System.out.println("YYYYMMdd 2022-01-01: " + df2.format(date3));
    }

3 SimpleDateFormat

使用Date对象时,用SimpleDateFormat进行格式化显示。SimpleDateFormat不是线程安全的,使用的时候,只能在方法内部创建新的局部变量

public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        // 日期格式化,输出:20230220
        System.out.println(df.format(date));
        
        try {
            String dateStr = "20230220";
            Date d = df.parse(dateStr);
            // 解析日期,输出:Mon Feb 20 00:00:00 CST 2023
            System.out.println(d);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

4 DateTimeFormatter

使用LocalDateTime或ZonedLocalDateTime时,要进行格式化显示,就要使用DateTimeFormatter。DateTimeFormatter是线程安全的,可以只创建一个实例,到处引用

 

public static void main(String[] args) throws ParseException {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
        String str = "2022.03.05";
        LocalDate localDate = LocalDate.parse(str, dateTimeFormatter);
        // 输出 2022-03-05
        System.out.println(localDate);

        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String dateStr = localDate.format(dateTimeFormatter1);
        // 2022/03/05
        System.out.println(dateStr);
    }
posted @ 2023-02-21 13:51  开坦克的舒克  阅读(329)  评论(0编辑  收藏  举报