How to convert String to Date in java?

Java 8Object Oriented ProgrammingProgramming

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

The SimpleDateFormat class has some additional methods, notably parse( ), which tries to parse a string according to the format stored in the given SimpleDateFormat object.

Example

 Live Demo

import java.text.SimpleDateFormat;
import java.util.Date;
public class DateString {
   public static void main(String args[]) throws Exception{
      String date = "21-9-2017";
      SimpleDateFormat sdt = new SimpleDateFormat("dd-MM-YYYY");
      Date result = sdt.parse(date);
      System.out.println(result);
   }
}

Output

Sun Jan 01 00:00:00 IST 2017
raja
Published on 12-Feb-2018 12:55:02
Advertisements