格式化类,日期时间以指定格式输出,DataFormat,SimpleDateFormat,DateTimeFormatter

1. DateFormat类

Date类打印输出时时间都是以默认的英文格式输出,如果以指定格式输出就需要DateFormat类。将特定格式的日期字符串转换成一个Date对象。DateFormat为抽象类不能被实例化,它提供了一系列方法获取实例对象。

Static DateFormat getDateInstance()

用于创建默认语言环境和格式化风格日期格式器

Static DateFormat getDateInstance(int style)

创建默认语言指定的格式日期格式器

Static DateFormat getDateTimeInstance()

创建默认语言环境和格式化风格的日期/时间格式其器

Static DateFormat getDateTimeInstance(int dateStyle,int timeStyle)

用于创建默认语言和指定格式风格的日期/时间格式器

String format(Date date)

将一个Date格式化为日期/时间字符串

Date parse(String source)

将字符串解析为一个日期

需要对Date类进行操作,输出用format解析为字符串,parse字符串解析为日期

Eg:

//创建Date对象

      Date date=new Date();

     

        //full格式的日期格式器对象

        DateFormat fullFormat=DateFormat.getDateInstance(DateFormat.FULL);

//long 格式的日期格式器对象

        DateFormat longFormat=DateFormat.getDateInstance(DateFormat.LONG);

//MEDIUM格式的日期/时间格式器对象

DateFormat mediumFormat=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

//short格式的日期/时间格式器对象

DateFormat shortFormat=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);

        System.out.println(date);

        System.out.println("完整格式:"+fullFormat.format(date));

        System.out.println("长格式:"+longFormat.format(date));

        System.out.println("普通格式:"+mediumFormat.format(date));

        System.out.println("短格式:"+shortFormat.format(date));

Eg2

   

 public static void main(String[] args) throws Exception{

    //转换格式需要抛出异常以防不备之需

        //创建Dateformat对象,和calendar测试对象

        Calendar c=Calendar.getInstance();

        DateFormat dt1=DateFormat.getDateInstance(DateFormat.SHORT);

        //创建Long格式的Dateformat对象

        DateFormat dt2=DateFormat.getDateInstance(DateFormat.LONG);

        //自定义两个日期格式的字符串

        String str1="2020-07-05";

        String str2="2020年07月05日";

        //输出对应字符串解析为date后的结果

        System.out.println(dt1.parse(str1));

        System.out.println(dt2.parse(str2));

        //calendar类转换为date类

        System.out.println(c.getTime());

        //data转换输出格式

        System.out.println(dt1.format(c.getTime()));

        System.out.println(dt2.format(c.getTime()));

    }

Calendar类用gettime()进行转换为Date类,输出类型看dateformat输出载体

2. SimpleDateFormat类

在使用DateFormat对象的parse()将字符串解析为Date日期,需要输入固定的字符串,显然不够灵活,为了更好的格式化日期,解析字符串Java提供了SimpleDateFormat类

可创造任何格式的时间格式

​
public static void main(String[] args) throws Exception {

        //自定义输入输出格式,不局限于short,long      

        SimpleDateFormat sdf= new SimpleDateFormat("Gyyyy/mm/dd");

        //让Date类按自定义格式输出

        System.out.println(sdf.format(Calendar.getInstance().getTime()));

       

        }

​

G Era 标志符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 
W 月份中的周数 Number 
D 年中的天数 Number 
d 月份中的天数 Number 
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; 
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 
k 一天中的小时数(1-24) Number 
K am/pm 中的小时数(0-11) Number 
h am/pm 中的小时数(1-12) Number
m 小时中的分钟数 Number 
s 分钟中的秒数 Number 
S 毫秒数 Number 
z 时区 General time zone Pacific Standard Time; 
Z 时区 RFC 

3. DateTimeFormatter类

该类相当于是Dateformat和SimpleDateformat的合体,可以把日期时间格式化为字符串,也可以把特定的字符串解析成日期时间。

       获取DateTimeFormatter对象方法有以下三种:

  1. 使用静态常量创建DateTimeFormatter格式器,在DateTimeFormatter类中包含大量的静态常量如BASIC_ISO_DATE,ISO_LOCAL_DATE,ISO_LOCAL_TIME等提供静态类获得其实例
  2. 使用不同风格的的枚举值来创建DateTimeFormatter格式器。在FormatStyle类中定义了FULL,LONG,MEDUIUM,SHORT四个枚举值,他们表示日期和时间的不同风格。
  3. 根据模式字符串创建DateTimeFormatter格式器。

使用DateTimeFormatter将日期时间格式化为字符串:

  1. 调用DateTimeFormatter的format(TemporalAccessor temporal)方法执行格式化。temporal 是一个TemporalAccessor接口其主要实现类有LocalDate,LocalDate Time。
  2. 调用LocalDate,LocalDateTime等日期时间对象的format(DateTimeFormatter formatter)方法执行格式化

示例:

          

public static void main(String[] args) {

        // TODO Auto-generated method stub

        LocalDateTime date=LocalDateTime.now();

    //1.使用常量创建DateTimeFormatter

        System.out.println("使用常量创建DateTimeFormatter:");

        DateTimeFormatter dtf1=DateTimeFormatter.ISO_DATE_TIME;

        System.out.println(dtf1.format(date));

    //2.使用MEDIUM类型风格的DateTimeFormatter

        System.out.println("使用MEDIUM类型风格的DateTimeFormatter:");

        DateTimeFormatter dtf2=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

        System.out.println(dtf2.format(date));

    //3.根据模式字母串来创建DateTimeFormatter格式器

        System.out.println("字母串来创建DateTimeFormatter格式器:");

        DateTimeFormatter dtf3=DateTimeFormatter.ofPattern("yyyy-m-dd");

        System.out.println(dtf3.format(date));



    }

输出结果:

使用常量创建DateTimeFormatter:

2021-07-05T16:15:16.244

使用MEDIUM类型风格的DateTimeFormatter:

2021-7-5 16:15:16

字母串来创建DateTimeFormatter格式器:

2021-7-05

如有错误联系作者

猜你喜欢

转载自blog.csdn.net/qq_52314655/article/details/119354656