提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用临时日期时间类的parse()方法来解析字符串。
import java.time.LocalDate; import java.time.LocalDateTime;/*时 代 J a v a 公 众 号 - nowjava.com*/ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; public class Main { public static void main(String[] args) { // Parse a string to form a Date-Time object LocalDate ld = LocalDate.parse("2013-12-28"); LocalDateTime ldt = LocalDateTime.parse("2013-12-28T08:44:00"); System.out.println("Parsed Date: " + ld); System.out.println("Parsed Date-Time: " + ldt); // Using a different Parser LocalDate ld2 = LocalDate.parse("2013-12-28", DateTimeFormatter.ISO_DATE); System.out.println("Different Parser: " + ld2); /* from 时代Java公众号*/ // Custom Parser String input = "12/28/2013"; try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate ld3 =