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 比较相等
732 2
LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相关操作
LocalDate、 LocalTime、 LocalDateTime以及ZonedDate、 ZonedTime、 ZonedDateTime相关操作
LocalDateTime、Date时间工具类
LocalDateTime、Date时间工具类
294 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的转换
953 0
10.2-10.3 datetime与时间格式的相互转换
Subclass relationships: 这类直接的对应关系 # object # timedate # tzinfo # time       #基本不使用 # date # datetime    #一般使用 datetime 取时间 import time for i in range(1,10):     print(i)     time.
626 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等