DateTimeFormatter

复制代码
package com.msb.test02;


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

/**
 * 开发人:liu
 * 日期:15:01:33
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class Test08 {
    //这是一个main方法:是程序的入口
    public static void main(String[] args) {
        //格式化类:DateTimeFormatter

        //方法1:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
        DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //df就可以帮助我们完成LocalDateTime和String之间的相互转换
        //localTateTime--->string
        LocalDateTime now = LocalDateTime.now();
        String st=df.format(now);
        System.out.println(st);//2022-10-14T15:52:13.152
        System.out.println("-----String--->LocalDateTime-----");
        //将String转为LocalDateTime
        TemporalAccessor parse = df.parse("2022-10-14T15:52:13.152");
        System.out.println(parse);
        System.out.println("---------------------");
        //方法2:本地化相关的格式。如:oflocalizedDateTime()
        //参数:FormatStyle.LONG  FormatStyle.MEDIUM   FormatStyle.SHORT
        //FormatStyle.LONG 2022年10月14日 下午04时54分37秒
        //FormatStyle.MEDIUM 2022-10-14 16:57:52
        //FormatStyle.SHORT 22-10-14 下午4:58
        DateTimeFormatter df1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        //LocalDateTime--->String
        LocalDateTime now1 = LocalDateTime.now();
        System.out.println(df1.format(now1));
        //String---->LocalDateTime
        TemporalAccessor now2=df1.parse("22-10-14 下午4:58");
        System.out.println("--------------------");
        //方法3:自定义的格式:如ofpattern("yyyy-mm-dd hh:mm:ss")--->重点,以后经常用
        DateTimeFormatter df3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        LocalDateTime now3 = LocalDateTime.now();
        //LocalDateTime---->Sting
        String format=df3.format(now3);
        System.out.println(format);
        //String---->LocalDateTime
        TemporalAccessor parse1 = df3.parse("2022-10-14 05:30:30");
        System.out.println(parse1);


    }
}
复制代码

 

posted @   爱的加勒比  阅读(301)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示