Java Program to convert a String to Date

Java 8Object Oriented ProgrammingProgramming

To convert string to date, you can use the parse method.

The following is our date format.

SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");

Let us convert our string value to date. The value in the parentheses is our string .

Date d = f.parse("13/11/2018");

Example

 Live Demo

import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
    public static void main(String[] args) throws Exception {
       SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
       Date d = f.parse("13/11/2018");
       System.out.println("Date: "+f.format(d));
    }
}

Output

Date: 13/11/2018
raja
Published on 12-Dec-2018 15:30:18
Advertisements