Java string conversion date
Since the date type in Java only has Date type, and there is date type and datetime type in Mysql. When we want to obtain datetime type data in Mysql in Java or insert datetime type data into the Mysql database, we need to do a conversion.
Java date format conversion
import ; import ; import ; public class Example { public static void main(String[] args) { String dateString = "2022-01-01"; // Create a DateTimeFormatter object, specify the date format DateTimeFormatter formatter = ("yyyy-MM-dd"); try { // Parses the string into a LocalDate object LocalDate date = (dateString, formatter); // Print LocalDate object (date); } catch (DateTimeParseException e) { ("Invalid date format"); (); } } }
In the above example, we first create aSimpleDateFormat
object, and specify the date format as"yyyy-MM-dd"
, which matches the format of the input string.
Then, we useparse()
Method parses the string into a date object. If the parsing is successful, aDate
Object, otherwise it will be thrownParseException
abnormal.
Finally, we print the date object and we can see that it is output in the default format.
It should be noted thatSimpleDateFormat
The date format pattern of the class is case sensitive. For example,"yyyy-MM-dd"
Indicates that the year is 4 digits, and the month and date are 2 digits. If the input string does not match the specified date format, it will be thrownParseException
abnormal.
also,SimpleDateFormat
The class also provides many other methods, such asformat()
Methods can format date objects into strings,setLenient()
The method can set whether the parsing process is loose, etc.
When usingSimpleDateFormat
When converting strings into date objects, you need to pay attention to the following points:
Date format mode
When specifying a date format, you need to select the appropriate date format mode according to the format of the input string.
Common date format patterns include:
- years:
yyyy
Represents a 4-digit year,yy
Represents a 2-digit year. - month:
MM
Represents a 2-digit month,M
Represents a 1-digit or 2-digit month. - date:
dd
Represents a 2-digit date,d
Represents a 1-digit or 2-digit date. - Hour:
HH
Represents a 24-hour clock 2-digit hours.H
Represents a 1-digit or 2-digit number of hours in a 24-hour system.hh
Represents a 2-digit number of hours in a 12-hour system.h
Represents a 1-digit or 2-digit number of hours in a 12-hour system. - minute:
mm
Represents a 2-digit minutem
Represents a 1-digit or 2-digit minutes. - Seconds:
ss
Represents 2-digit seconds.s
Represents 1 or 2 digits seconds. - millisecond:
SSS
Represents 3-digit milliseconds. - AM/PM:
a
Indicates morning/afternoon marks.
Date formatting
In addition to converting strings to date objects,SimpleDateFormat
The class also providesformat()
Method, used to format date objects into strings. You can specify different date format patterns as needed to convert date objects to strings of a specific format.
To sum up, useSimpleDateFormat
Classes can conveniently convert strings into date objects, but pay attention to the selection of date format patterns, exception handling, and thread safety.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.