DateTimeFormatter类

package com.Vapour.String;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class Example28 {
    public static void main(String[] args) {
        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-MM-dd HH:mm:ss");
        System.out.println(date.format(dtf3));
    }
}

package com.Vapour.String;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Example29 {
    public static void main(String[] args) {
        //定义两种日期格式的字符串
        String str1 = "2018-01-30 12:43:34";
        String str2 = "2018年01月30日 12时43分34秒";
        //定义解析所用的格式器
        DateTimeFormatter formatter1 = DateTimeFormatter
                                        .ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter2= DateTimeFormatter
                                        .ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
        //使用LocalDateTime的parse()方法执行解析
        LocalDateTime localDateTime1 = LocalDateTime
                                                    .parse(str1,formatter1);
        LocalDateTime localDateTime2 = LocalDateTime
                                                    .parse(str2,formatter2);
        //输出结果
        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值