SimpleDateFormat和DateTimeFormatter

SimpleDateFormat和DateTimeFormatter都是进行日期时间格式化的工具类,后者是为jdk1.8以后的日期对象服务的,它没有线程安全的问题;而前者,是存在多线程下的安全隐患的。

作用

  1. 将日期格式化成日期/时间字符串
  2. 从给定字符串的开始解析文本以生成日期
  3. SimpleDateFormat是针对java.util.date和java.sql.date进行操作
  4. DateTimeFormatter是针对jdk1.8新增日期API进行转换操作

SimpleDateFormat格式化和解析日期

  1. 格式化(从Date到String)
  public final String format(Date date):将日期格式化成日期/时间字符串
  1. 解析(从String到Date)
  public Date pase(String source):从给定字符串的开始解析文本以生成日期
  1. 具体代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class SimpleDateFormatDemo {
    public static void main(String[] args) throws ParseException {
        //格式化:从Date到String
        Date d = new Date();//获取当前系统的时间
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String s = sdf.format(d);
        System.out.println(s);
        System.out.println("--------");
 
        //解析:从String到Date
        String ss = "2022-03-20 20:20:20";
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dd = sdf2.parse(ss);
        System.out.println(dd);
    }
}

DateTimeFormatter格式化和解析日期

  1. ofPattern解析到日期对象
  2. format格式化成字段符
  3. 具体代码
public class Test10 {
    //这是一个main方法,是程序的入口:
    public static void main(String[] args) {
        //格式化类:DateTimeFormatter
        //方式一:预定义的标准格式。如: ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;IS0_LOCAL_TIME
        DateTimeFormatter df1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //df1就可以帮我们完成LocalDateTime和String之间的相互转换:
        //LocalDateTime-->String:
        LocalDateTime now = LocalDateTime.now();
        String str = df1.format(now);
        System.out.println(str);//2020-06-15T15:02:51.29
        //String--->LocalDateTime
        TemporalAccessor parse = df1.parse("2020-06-15T15:02:51.29");
        System.out.println(parse);
        //方式二:本地化相关的格式。如: oflocalizedDateTime()
        //参数:FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT
        //FormatStyle.LONG :2020年6月15日 下午03时17分13秒
        //FormatStyle.MEDIUM: 2020-6-15 15:17:42
        //FormatStyle.SHORT:20-6-15 下午3:18
        DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //LocalDateTime-->String:
        LocalDateTime now1 = LocalDateTime.now();
        String str2 = df2.format(now1);
        System.out.println(str2);
        //String--->LocalDateTime
        TemporalAccessor parse1 = df2.parse("20-6-15 下午3:18");
        System.out.println(parse1);
        //方式三: 自定义的格式。如: ofPattern( "yyyy-MM-dd hh:mm:ss") ---》重点,以后常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //LocalDateTime-->String:
        LocalDateTime now2 = LocalDateTime.now();
        String format = df3.format(now2);
        System.out.println(format);//2020-06-15 03:22:03
        //String--->LocalDateTime
        TemporalAccessor parse2 = df3.parse("2020-06-15 03:22:03");
        System.out.println(parse2);
    }
}

如果使用SimpleDateFormat想做到线程安全,则可以使用ThreadLocal来实现

    private static final ThreadLocal<SimpleDateFormat> dateTimeFormatThreadLocal = ThreadLocal
            .withInitial(() -> new SimpleDateFormat(DEFAULT_DATE_FORMAT));
posted @   张占岭  阅读(313)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2022-04-27 springboot~自定义valid及DefaultGroupSequenceProvider的作用
2021-04-27 docker~添加hosts绑定的方法
2019-04-27 dev和master合并冲突解决
2018-04-27 java~IDEA引用包时分组所有java包
2017-04-27 基础才是重中之重~关于ThreadStatic和Quartz的一点渊源
2016-04-27 知方可补不足~SqlServer自动备份数据库及清理备份文件
2015-04-27 MongoDB学习笔记~为IMongoRepository接口添加了排序和表达式树,针对官方驱动
点击右上角即可分享
微信分享提示