继Date,Calendar之后的日期操作类(一)

继续日期类的学习DateTimeFormatter----
异常—java.time.format.DateTimeParseException:

字符产转换为时间类的第二种方式
第一种见链接----

https://blog.csdn.net/weixin_54061333/article/details/118581212

package baozhuang;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * @author : Gavin
 * @date: 2021/7/8 - 07 - 08 - 19:19
 * @Description: Date
 * @version: 1.0
 */
public class CESHI {
    public static void main(String[] args) {
       //日期格式化类---DateTimeFormatter
//        预定义的标准格式
        DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //System.out.println(dateTimeFormatter);
    LocalDateTime localDate= LocalDateTime.now();
    String str=localDate.format(dateTimeFormatter);
        System.out.println(str);

        String---->LocalDateTime字符串转换为时间---要了解定义时间的标准格式
       TemporalAccessor par=dateTimeFormatter.parse("2021-07-10T12:46:52.5711012");//这里格式一定,不符合格式不行
       //TemporalAccessor par1=dateTimeFormatter.parse("2021-07-10  2:46:52.5711012");//报错-- java.time.format.DateTimeParseException:
        System.out.println(par);
        //System.out.println(par1);
    }
}

这里要注意的是要知道标准格式是什么样的形式,如果字符串格式不会,那么会出现java.time.format.DateTimeParseException:,

先看一下别的
日期转换为字符串的其他方式–比较常用;


package baozhuang;

        import java.time.LocalDate;
        import java.time.LocalDateTime;
        import java.time.ZoneId;
        import java.time.format.DateTimeFormatter;
        import java.time.format.FormatStyle;
        import java.time.temporal.TemporalAccessor;
        import java.util.Date;

/**
 * @author : Gavin
 * @date: 2021/7/8 - 07 - 08 - 19:19
 * @Description: Date
 * @version: 1.0
 */
public class CESHI {
    public static void main(String[] args) {
        //日期格式化类---DateTimeFormatter
//        预定义的标准格式
        DateTimeFormatter dateTimeFormatter= DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //System.out.println(dateTimeFormatter);
        LocalDateTime localDate= LocalDateTime.now();
        String str=localDate.format(dateTimeFormatter);
        System.out.println(str);

        String---->LocalDateTime字符串转换为时间---要了解定义时间的标准格式
        TemporalAccessor par=dateTimeFormatter.parse("2021-07-10T12:46:52.5711012");//这里格式一定,不符合格式不行
        //TemporalAccessor par1=dateTimeFormatter.parse("2021-07-10  2:46:52.5711012");//报错-- java.time.format.DateTimeParseException:
        System.out.println(par);
        //System.out.println(par1);

        //本地化时间格式

        DateTimeFormatter dateTimeFormatter2=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.systemDefault());
        LocalDateTime localDateTime=LocalDateTime.now();
        String format = dateTimeFormatter2.format(localDateTime);
        System.out.println(dateTimeFormatter2);
        System.out.println(format);
//
       DateTimeFormatter dateTimeFormatter3=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.systemDefault());
        LocalDateTime localDateTime1=LocalDateTime.now();
        String format1 = dateTimeFormatter3.format(localDateTime1);
        System.out.println(dateTimeFormatter3);
        System.out.println(format1);
//
        DateTimeFormatter dateTimeFormatter5=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.systemDefault());
        LocalDateTime localDateTime5=LocalDateTime.now();
        String format3 = dateTimeFormatter5.format(localDateTime5);
        System.out.println(dateTimeFormatter5);
        System.out.println(format3);
      DateTimeFormatter dateTimeFormatter4=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
        LocalDateTime localDateTime2=LocalDateTime.now();
        String format2 = dateTimeFormatter4.format(localDateTime2);
        System.out.println(dateTimeFormatter4);
        System.out.println(format2);

    }
}

Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
2021-07-10T13:43:57.680317
{},ISO resolved to 2021-07-10T12:46:52.571101200
Localized(FULL,FULL)
2021710日星期六 格林尼治标准时间 下午1:43:57
Localized(LONG,LONG)
2021710日 CST 下午1:43:57
Localized(MEDIUM,MEDIUM)
2021710日 下午1:43:57
Localized(SHORT,SHORT)
2021/7/10 下午1:43

withZone(ZoneId.of(“UTC”))指定时区,在JDK1.8之后必须要指定时区,否则会运行时报错–Exception in thread “main“ java.time.DateTimeException: Unable to extract ZoneId from temporal
每一种字符串格式要对应起来
例如–

 DateTimeFormatter dateTimeFormatter4=DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
        LocalDateTime localDateTime2=LocalDateTime.now();
        String format2 = dateTimeFormatter4.format(localDateTime2);
        System.out.println(dateTimeFormatter4);
        System.out.println(format2);
TemporalAccessor temporalAccessor=dateTimeFormatter4.parse("2021年7月10日星期六 格林尼治标准时间 下午1:43:57");
        System.out.println(temporalAccessor);
Exception in thread "main" java.time.format.DateTimeParseException: Text '2021年7月10日星期六 格林尼治标准时间 下午1:43:57' could not be parsed at index 4
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1879)

那么有没有自定义格式的方法呢?肯定有呀.下一篇继续----我写下一篇的目的主要是为了复习之前学的…

日拱一兵,只进不退

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CodeMartain

祝:生活蒸蒸日上!

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

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

打赏作者

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

抵扣说明:

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

余额充值