Java中的格式化类——DateTimeFormatter类

JDK 8在java.time.format包下还提供了一个DateTimeFormatter类,
该类也是一个格式化类,其功能相当于DataFormat和SimpleDateFormat的合体,
它不仅可以将日期、时间对象格式化成字符串,
还能将特定格式的字符串解析成日期、时间对象。
要使用DateTimeFormatter进行格式化或者解析,就必须先获得DateTimeFormatter对象。

DateTimeFomatter类——获取实例对象方:
1、使用静态常量创建DateTimeFormatter格式器。
在DateTimeFormatter类中包含大量的静态常量,如BASIC_ISO_DATE、ISO_LOCAL_DATE、ISO_LOCAL_TIME等,
通过这些静态常量都可以获取DateTimeFormatter实例。

2、使用不同风格的枚举值来创建DateTimeFormatter格式器。
在FormatStyle类中定义了FULL、LONG、MEDIUM和SHORT四个枚举值,它们表示日期和时间的不同风格。

3、根据模式字符串创建DateTimeFormatter格式器。
使用DateTimeFomatter将日期时间格式化为字符串,可以通过以下两种方法

1、调用DateTimeFormatter的format(TemporalAccessor temporal)方法执行格式化。
其中参数temporal是一个TemporalAccessor类型接口,其主要实现类有LocalDate、LocalDateTime。

2、调用LocalDate、LocalDateTime等日期、时间对象的format(DateTimeFormatter formatter)方法执行格式化。
完成时间日期类的格式化

package changYongLei;

import java.time.*;
import java.time.format.*;

//完成日期、时间格式化
public class GeShiHuaLei_DateTimeFormatter {
   public static void main(String[] args) {
    LocalDateTime date = LocalDateTime.now();
    //1.使用常量创建DateTimeFormatter
    DateTimeFormatter dtf1 = DateTimeFormatter.ISO_DATE_TIME;
    System.out.println("使用常量创建DateTimeFormatter:"+dtf1.format(date));
    //2.使用MEDIUM类型风格的DateTimeFormatter
    DateTimeFormatter dtf2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    System.out.println("使用MEDIUM类型风格的DateTimeFormatter:"+dtf2.format(date));
    //3.根据模式字符串来创建DateTimeFormatter格式器
    DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");
    System.out.println("根据模式字符串来创建DateTimeFormatter格式器:"+date.format(dtf3));
    System.out.println("根据模式字符串来创建DateTimeFormatter格式器:"+dtf3.format(date));
   }
}

在这里插入图片描述

解析字符串
可以通过日期时间对象所提供的parse(CharSequence text, DateTimeFormatter formatter)方法来实现。
解析字符串

package changYongLei;

import java.time.LocalDateTime;
import java.time.format.*;

//解析字符串
public class GeShiHuaLei_DateTimeFormatter2 {
  public static void main(String[] args) {
   //定义两种日期格式的字符串
   String str1 = "2020-05-20 12:36:34";
   String str2 = "2020年05月20日 12点36分34秒";
   //定义解析所用的格式器
   DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
   DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH点mm分ss秒");
   //使用DateTimeFormatter的parse()方法执行解析
   LocalDateTime ldt1 = LocalDateTime.parse(str1, dtf1);
   LocalDateTime ldt2 = LocalDateTime.parse(str2, dtf2);
   //输出
   System.out.println(ldt1);
   System.out.println(ldt2);
  }
}

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用 `java.time.format.DateTimeFormatter` 类将 `java.util.Date` 类格式化成 `String` 类型。 例如,如果要将日期格式化为 "yyyy-MM-dd" 格式,可以这样做: ``` import java.time.format.DateTimeFormatter; import java.util.Date; Date date = new Date(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String dateString = formatter.format(date.toInstant()); ``` 如果要将时间格式化为 "HH:mm:ss" 格式,可以这样做: ``` import java.time.format.DateTimeFormatter; import java.util.Date; Date date = new Date(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); String dateString = formatter.format(date.toInstant()); ``` 注意,上面的例子使用了 `java.util.Date.toInstant()` 方法将 `java.util.Date` 对象转换为 `java.time.Instant` 对象。 具体的模式格式可以参考jdk文档,也可以自定义 ### 回答2: 在Java,我们可以使用DateTimeFormatter类来将Date对象格式化为String类型。DateTimeFormatter类是java.time包的一个类,用于格式化日期和时间。 下面是使用DateTimeFormatter将Date对象格式化为String类型的步骤: 步骤1:导入必要的包和类。 ```java import java.time.format.DateTimeFormatter; import java.util.Date; ``` 步骤2:创建一个DateTimeFormatter对象。 ```java DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); ``` 在这个例子,我们使用 "yyyy-MM-dd" 作为日期的格式,其 "yyyy" 表示四位数的年份,"MM" 表示两位数的月份,"dd" 表示两位数的日期。 步骤3:将Date对象转换为LocalDate对象。 ```java LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); ``` 在这个例子,我们使用Date对象的toInstant()方法将其转换为Instant对象,然后使用atZone()方法将其转换为ZoneId.systemDefault()时区的ZonedDateTime对象,最后使用toLocalDate()方法将其转换为LocalDate对象。 步骤4:使用DateTimeFormatter对象的format()方法将LocalDate对象格式化为String类型。 ```java String formattedDate = localDate.format(formatter); ``` 现在,我们已经成功将Date对象格式化为String类型。formattedDate变量将包含格式化后的日期字符串,可以根据需要进行使用。 注意:Java 8之前的版本,可以使用SimpleDateFormat类来格式化Date对象为String类型,但在Java 8及以后的版本,推荐使用新的日期和时间API(java.time包)提供的DateTimeFormatter类来完成格式化操作。 ### 回答3: 在Java,我们可以使用DateTimeFormatter类将Date类格式化为String类型。DateTimeFormatter类是在Java 8引入的,用于格式化日期和时间对象。 要使用DateTimeFormatter将Date类格式化为String类型,我们需要执行以下步骤: 1. 导入所需的库: ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; ``` 2. 创建Date对象: ```java Date date = new Date(); ``` 3. 将Date对象转换为LocalDateTime对象: ```java LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); ``` 4. 创建DateTimeFormatter对象并定义所需的格式: ```java DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); ``` 5. 使用DateTimeFormatter对象将LocalDateTime对象格式化为String类型: ```java String formattedDate = localDateTime.format(formatter); ``` 现在,我们得到了字符串格式化的日期。可以根据需要更改DateTimeFormatter对象的格式模式以获得不同的日期和时间格式。例如,如果我们想要一个只有日期的格式,可以使用"yyyy-MM-dd"作为格式模式。 最后,可以通过输出formattedDate变量来查看格式化后的字符串日期: ```java System.out.println(formattedDate); ``` 请注意,使用Java 8的新日期和时间API会更简洁和安全地处理日期和时间对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值