[Java] [It is convenient to remember!!!] How to convert from LocalDate type to character string and from character string to LocalDate type

1 minute read

1. Prior knowledge

As prior knowledge, the contents of the above link are required.

2. How to convert from LocalDate type to character string and from character string to LocalDate type

01.png 01.png

  1. Start Eclipse and select [File (F)] → [New (N)] → [Java project]. 02.png
  2. Enter LocalDateTest in the project name (P), select JavaSE-1.8 for Use JRE (V), and click the Finish > button. 03.png
  3. Right-click on [src] and select [New]→[Class]. : 04.png
  4. Enter Test for the name, check public static void main(String[] args)(V), and click the Finish button.

Test.java




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

public class Test {

public static void main(String[] args) {

// the string to convert
String date1 = "2020/07/07";

// Convert from String to java.time.LocalDate
LocalDate test1 = LocalDate.parse(date1, DateTimeFormatter.ofPattern("yyyy/MM/dd"));

// convert java.time.LocalDate to String
String test2 = test1.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

// display the conversion contents
System.out.println(date1 + "→ "+ test1);
System.out.println(test1 + "→ "+ test2);

}

}

  1. Input the above source code into Test.java. 06.png
  2. Right-click Test.java and select [Run]→[2 Java application]. : 05.png
  3. Success if displayed like the image. ‥

Tags:

Updated: