The mutual conversion techniques of the Date class and the String class in Java

In Java, the Date and String classes are two commonly used classes for processing date and time.In the actual development process, we often need to change between these two categories.This article will introduce some techniques for the Date and String class in the Java and the String class, and explain the example code. 1. Date converted to string: The Tostring () method in the Date class returns a string containing the date and time information, but its format is not easy to use directly.Therefore, we can use the SimpleDateFormat class to convert the Date object into a string of a specific format.The following is an example: import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateToStringExample { public static void main(String[] args) { // Create a date object of the current time Date currentDate = new Date(); // Create SimpleDateFormat objects, specify the date format DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Use the format () method of using SimpleDateFormat to convert the date to string String dateString = dateFormat.format(currentDate); // Print the conversion string System.out.println ("Date converted to string:" + datestring); } } In the above example, we created a DATE object CurrentDate of the current time, and used the SimpleDateFormat class to create a specified format DateFormat of the date format of "YYYYY-MM-DD HH: MM: SS".Then, we use DateFormat's format () method to convert the date object to a string, and finally print the string that is converted to the conversion. 2. String into date: To convert the string to Date object, we need to define a SimpleDateFormat object corresponding to the string format, and then use its PARSE () method to convert the string to the Date object.The following is an example: import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample { public static void main(String[] args) { // Define a date string String dateString = "2022-01-01 12:00:00"; // Create SimpleDateFormat objects, specify the date format DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { // Use the PARSE () method of SimpleDateFormat to convert the string to date to date Date date = dateFormat.parse(dateString); // Print the conversion Date object System.out.println ("String to date:" + date); } catch (ParseException e) { e.printStackTrace(); } } } In the above example, we define a date string datestring, and use the SimpleDateFormat class to create a specified format DateFormat with the date format of the dating object "Yyyy-MM-DD HH: MM: SS".Then, we convert the date string to the date object by calling the PARSE () method of DateFormat, and finally print the Date object after the output conversion. Summarize: Through the above examples, we can see that in Java, the DateDateFormat class needs to be used to convert the DateDateFormat class. The date () method is converted to string through the Format () method, and the string is converted to date through the PARSE () method.In actual development, select the appropriate date format as needed and perform corresponding conversion operations.These techniques can help us handle the tasks related to date and time.