当前位置:网站首页>[datetmeformatter] realize the conversion between localdatetime and text

[datetmeformatter] realize the conversion between localdatetime and text

2022-06-12 14:36:00 Understand the principle + good code skills

package org.example.testTime;

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

public class TestDateTimeFormatter {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        // step1.LocalDateTime Convert to this format 
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String str = dtf.format(now);
        System.out.println(str); // 2022-02-18 01:09:19

        // step2. Text to LocalDateTime
        LocalDateTime time = LocalDateTime.parse(str, dtf); // 2022-02-18T01:09:19
        System.out.println(time);
    }
}

原网站

版权声明
本文为[Understand the principle + good code skills]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010504207407.html