String to date type conversion
package com.shxt.demo02; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Demo18 { public static void main(String[] args) { LocalDateTime date = LocalDateTime.now(); DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); //日期转字符串 String str = date.format(format1); System.out.println("日期转换为字符串:"+str); DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); //字符串转日期 LocalDate date2 = LocalDate.parse(str,format2); System.out.println("日期类型:"+date2); } }
The above is the detailed content of Example analysis of converting string to date type in Java. For more information, please follow other related articles on the PHP Chinese website!