DateTimeFormatter 和 LocalDateTime 日期转换

简介: 将字符串转换为Date类型,Date转换为字符串。

package com.hr.util.test.date;

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

public class DateTimeFormatterTest {

public static void main(String[] args) {
    //将字符串转换为Date类型
    String dateStr = "2020-08-18";
    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    LocalDate parse = LocalDate.parse(dateStr, timeFormatter);
    System.out.println(parse);

    //Date转换为字符串
    LocalDateTime date = LocalDateTime.now();
    System.out.println(date);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
    String format = date.format(formatter);
    System.out.println(format);
}
AI 代码解读

}

目录
打赏
0
0
0
0
0
分享
相关文章
localdatetime 比较相等
localdatetime 比较相等
731 2
日期时间之争:Date与LocalDateTime的决胜局
日期时间之争:Date与LocalDateTime的决胜局
1940 0
LocalDateTime、Date时间工具类
LocalDateTime、Date时间工具类
293 0
使用 SimpleDateFormat 格式化日期
使用 SimpleDateFormat 格式化日期
144 0
日期时间类(Date、DateFormat、Calendar)
日期时间类(Date、DateFormat、Calendar)
216 1
拿到指定时间对象
拿到指定时间对象
113 0
有关日期格式化的问题
有关日期格式化的问题
116 0
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
209 0
2hutool源码分析:DateUtil(时间工具类)-常用的时间类型Date,DateTime,Calendar和TemporalAccessor(LocalDateTime)转换
LocalDate、LocalDateTime与timestamp、Date的转换
LocalDate、LocalDateTime与timestamp、Date的转换
952 0

热门文章

最新文章