java格式化类DateTimeFormatter的使用

package com.test.test02;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

public class Test10 {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
        //格式化类:DateTimeFormatter

        //方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME; ISO_LOCAL_DATE  ISO_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); //2023-05-09T14:22:07.687

        //String--->LocalDateTime
        TemporalAccessor parse = df1.parse("2023-05-09T14:22:07.687");
        System.out.println(parse);//{},ISO resolved to 2023-05-09T14:22:07.687


        //方式二:本地化相关的格式。如:oflocalizedDateTime()
        //参数:FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT
        //FormatStyle.LONG:2023年5月9日 下午02时35分39秒   LONG是这样的格式
        //FormatStyle.MEDIUM:2023-5-9 14:37:05 MEDIUM是这样的格式
        //FormatStyle.SHORT:23-5-9 下午2:37 SHORT是这样的格式
        DateTimeFormatter df2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //LocalDateTime--->String
        LocalDateTime now1 = LocalDateTime.now();
        String str1 = df2.format(now1);
        System.out.println(str1); //2023年5月9日 下午02时35分39秒 2023-5-9 14:37:05  23-5-9 下午2:37

        //String--->LocalDateTime 类型需要对应,下面的不是short类型运行就不对了
        TemporalAccessor parse1 = df2.parse("23-5-9 下午2:37");
        System.out.println(parse1); //{},ISO resolved to 2023-05-09T14:37



        //方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss") --->重点,以后常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //LocalDateTime--->String
        LocalDateTime now2 = LocalDateTime.now();
        String str2 = df3.format(now2);
        System.out.println(str2); //2023-05-09 02:48:56
        //String--->LocalDateTime
        TemporalAccessor parse2 = df3.parse("2023-05-09 02:48:56");
        System.out.println(parse2);
        //{HourOfAmPm=2, MinuteOfHour=48, MilliOfSecond=0, NanoOfSecond=0, SecondOfMinute=56, MicroOfSecond=0},ISO resolved to 2023-05-09


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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型。DateTimeFormatterjava.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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值