Home > Java > javaTutorial > Example analysis of converting string to date type in Java

Example analysis of converting string to date type in Java

王林
Release: 2023-05-10 17:37:06
forward
1340 people have browsed it

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);     
}  
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template