Convert String to Date in Java

Given a String in date format, the task is to convert this String into actual Date.

Examples:

Input: string = "2018-10-28T15:23:01Z"
Output: 2018-10-28T15:23:01Z

Input: string = "28 October, 2018"
Output: 2018-10-28
  • Method 1: Using Instant Class. Instant class in java.time package gives nanosecond accuracy. It is similar to Date class but gives better accuracy.

    Approach:



    1. Get the String to be converted.
    2. Create an empty Instant timestamp object
    3. Covnert the String to Date using Instant.parse() method.
    4. If converted successfully, then print the Date
    5. If not converted successfully, then DateTimeParseException is thrown.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String to Date
      
    import java.time.Instant;
    import java.time.format.DateTimeParseException;
      
    class GFG {
      
        // Function tot convert String to Date
        public static Instant getDateFromString(String string)
        {
      
            // Create an Instant object
            Instant timestamp = null;
      
            // Parse the String to Date
            timestamp = Instant.parse(string);
      
            // return the converted timestamp
            return timestamp;
        }
      
        public static void main(String[] args)
        {
            // Get the String
            String string = "2018-10-28T15:23:01Z";
      
            // Get the Date from String
            try {
      
                Instant timestamp = getDateFromString(string);
      
                // Print the converted Date
                System.out.println(timestamp);
            }
      
            // Throws DateTimeParseException
            // if the string cannot be parsed
            catch (DateTimeParseException e) {
      
                System.out.println("Exception: " + e);
            }
        }
    }

    chevron_right

    
    

    Output:

    2018-10-28T15:23:01Z
    
  • Method 2: Using DateTimeFormatter Class.

    Approach:

    1. Get the String to be converted and the required format.
    2. Create an empty LocalDate object
    3. Covnert the String to Date using LocalDate.parse() method.
    4. If converted successfully, then print the Date
    5. If the String pattern is invalid, then IllegalArgumentException is thrown.
    6. If not converted successfully, then DateTimeParseException is thrown.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert String to Date
      
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
      
    class GFG {
      
        // Function tot convert String to Date
        public static LocalDate
        getDateFromString(String string, DateTimeFormatter format)
        {
      
            // Convert the String to Date in the specified format
            LocalDate date = LocalDate.parse(string, format);
      
            // return the converted date
            return date;
        }
      
        public static void main(String[] args)
        {
      
            // Get the String
            String string = "28 October, 2018";
      
            // Get the Format
            DateTimeFormatter format = DateTimeFormatter
                                           .ofPattern("d MMMM, yyyy");
      
            try {
      
                // Get the Date from String
                LocalDate date = getDateFromString(string, format);
      
                // Print the converted date
                System.out.println(date);
            }
      
            // If the String pattern is invalid
            catch (IllegalArgumentException e) {
      
                System.out.println("Exception: " + e);
            }
      
            // If the String was unable to be parsed.
            catch (DateTimeParseException e) {
      
                System.out.println("Exception: " + e);
            }
        }
    }

    chevron_right

    
    

    Output:

    2018-10-28
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.