SoFunction
Updated on 2025-04-10

How to convert Java strings to date

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 aSimpleDateFormatobject, 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, aDateObject, otherwise it will be thrownParseExceptionabnormal.

Finally, we print the date object and we can see that it is output in the default format.

It should be noted thatSimpleDateFormatThe 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 thrownParseExceptionabnormal.

also,SimpleDateFormatThe 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 usingSimpleDateFormatWhen 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:yyyyRepresents a 4-digit year,yyRepresents a 2-digit year.
  • month:MMRepresents a 2-digit month,MRepresents a 1-digit or 2-digit month.
  • date:ddRepresents a 2-digit date,dRepresents a 1-digit or 2-digit date.
  • Hour:HHRepresents a 24-hour clock 2-digit hours.HRepresents a 1-digit or 2-digit number of hours in a 24-hour system.hhRepresents a 2-digit number of hours in a 12-hour system.hRepresents a 1-digit or 2-digit number of hours in a 12-hour system.
  • minute:mmRepresents a 2-digit minutemRepresents a 1-digit or 2-digit minutes.
  • Seconds:ssRepresents 2-digit seconds.sRepresents 1 or 2 digits seconds.
  • millisecond:SSSRepresents 3-digit milliseconds.
  • AM/PM:aIndicates morning/afternoon marks.

Date formatting

In addition to converting strings to date objects,SimpleDateFormatThe 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, useSimpleDateFormatClasses 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.