How to Convert String to Date in Java

Java Program to covert String to Date

Java Strings

  • A Java object that represents a string of characters is called a string. Strings are frequently employed to store text data like names, addresses, words, etc. A String class that comes with Java offers methods for comparing and manipulating strings.
  • Java has a special memory area called the string pool, where string objects are stored. If two strings have the same value, only one object will be created and stored in the string pool.

To know more about Java Program to Convert String to Date, read the complete article.

Steps to Convert String to Date in Java

  • To convert a string to a date in Java, you can follow these steps:

    • Import the java.text.SimpleDateFormat class in your Java program.

    • Create an instance of the SimpleDateFormat class with a desired date format pattern.

    • Use the parse() method of the SimpleDateFormat class to parse the string into a Date object.

    • If necessary, use the getTime() method of the Date class to get the time in milliseconds.

Let’s look at a Java Program to Convert String to Date to perform certain operations.

Example 1: Java Program to Convert String to Date. 

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

public class Main {
  public static void main(String[] args) {
    String dateString = "2023-02-08";
    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
    LocalDate date = LocalDate.parse(dateString, formatter);
    System.out.println("Date: " + date);
  }
}

Output

Date: 2023-02-08

Example 2 : Java Program to Convert String to Date using predefined formatters 

Run
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    String dateString = "2023-02-08";
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    try {
      Date date = formatter.parse(dateString);
      System.out.println("Date: " + date);
    } catch (ParseException e) {
      System.out.println("Exception while parsing the date: " + e.getMessage());
    }
  }
}

Output

Date: Wed Feb 08 00:00:00 GMT 2023

Example 3: Java Program Convert String to Date using pattern formatters

Run
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    String dateString = "07-Feb-2023";
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    try {
      Date date = formatter.parse(dateString);
      System.out.println("Date: " + date);
    } catch (ParseException e) {
      System.out.println("Exception while parsing the date: " + e.getMessage());
    }
  }
}

Output

Date: Tue Feb 07 00:00:00 GMT 2023

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription