DateTimeFormatter(代替SimpleDateFormat):用于时间的格式化和解析

为什么DateTimeFormatter要代替SimpleDateFormat?

因为SimpleDateFormat是线程不安全的,而DateTimeFormatter是线程安全的。

请添加图片描述

1.创建日期格式化器对象

//1.创建一个日期格式化器对象出来
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        //formatter是格式化对象,后续想把时间格式化要用到这个对象的format方法,返回值是String类型的
        System.out.println(formatter);
        System.out.println("-------------------------");

2.对时间进行格式化

 //2.对时间进行格式化
        LocalDateTime now=LocalDateTime.now();
        System.out.println(now);
        //是拿格式化器的格式化时间
        String rs=formatter.format(now);
        System.out.println(rs);
        System.out.println(formatter.format(now));
        System.out.println("-------------------------");

3.格式化时间的另一种方案

//3.格式化时间的另一种方案
        //是拿时间格式化自己
        String rs2=now.format(formatter);
        System.out.println(rs2);

4.使用LocalDateTime解析时间

//解析时间:解析时间一般使用 LocalDateTime提供的解析方法来解析
        String dateStr="2029年12月12日 12:12:11";
        LocalDateTime ldt=LocalDateTime.parse(dateStr,formatter);
        System.out.println(ldt);

5.完整代码

package com.API1;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
//目标:掌握JDK8新增的DateTimeFormatter格式化器的用法
public class test_DateTimeFormatter {
    public static void main(String[] args) {
        //1.创建一个日期格式化器对象出来
        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        //formatter是格式化对象,后续想把时间格式化要用到这个对象的format方法,返回值是String类型的
        System.out.println(formatter);
        System.out.println("-------------------------");

        //2.对时间进行格式化
        LocalDateTime now=LocalDateTime.now();
        System.out.println(now);
        //是拿格式化器的格式化时间
        String rs=formatter.format(now);
        System.out.println(rs);
        System.out.println(formatter.format(now));
        System.out.println("-------------------------");

        //3.格式化时间的另一种方案
        //是拿时间格式化自己
        String rs2=now.format(formatter);
        System.out.println(rs2);

        //解析时间:解析时间一般使用 LocalDateTime提供的解析方法来解析
        String dateStr="2029年12月12日 12:12:11";
        LocalDateTime ldt=LocalDateTime.parse(dateStr,formatter);
        System.out.println(ldt);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
DateTimeFormatterSimpleDateFormat用于日期和时间格式化的类,但它们有一些重要的区别。 1. 线程安全性: - SimpleDateFormat不是线程安全的。如果多个线程同时使用同一个SimpleDateFormat对象进行日期和字符串的转换,可能会导致异常。 - DateTimeFormatter是线程安全的。多个线程可以同时使用同一个DateTimeFormatter对象进行日期和字符串的转换,而不会出现线程安全问题。 2. API设计: - SimpleDateFormatJava早期提供的日期和时间格式化类,其API设计相对简单。 - DateTimeFormatterJava 8引入的新类,其API设计更加灵活和强大。它提供了丰富的方法来处理日期和时间格式化解析,可以满足更多的需求。 3. 格式化模式: - SimpleDateFormat使用一种基于模式字符串的方式来定义日期和时间格式化模式。例如,"yyyy-MM-dd HH:mm:ss"表示年份-月份-日期 小时:分钟:秒。 - DateTimeFormatter使用一种基于模式字符的方式来定义日期和时间格式化模式,但它还提供了一些预定义的格式化模式,如ISO_DATE、ISO_TIME、ISO_DATE_TIME等。 4. 性能: - SimpleDateFormat的性能相对较低,特别是在多线程环境下。 - DateTimeFormatter的性能相对较高,特别是在多线程环境下。它使用了一些优化技术,如缓存和线程安全的解析器。 综上所述,DateTimeFormatter相比SimpleDateFormat具有更好的线程安全性、更灵活的API设计和更高的性能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值