Parse string date value input in Java

Java 8Object Oriented ProgrammingProgramming

UseSimpleDateFormat('dd-MMM-yy') for string date.

Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");

For above class, do not forget to import the following package, else an error would be visible.

import java.text.SimpleDateFormat;

Now, parse the date.

Date dt = (Date) dateFormatter.parseObject("20-Nov-18");

Example

 Live Demo

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
   public static void main(String[] argv) throws Exception {
      Format dateFormatter = new SimpleDateFormat("dd-MMM-yy");
      // parse
      Date dt = (Date) dateFormatter.parseObject("20-Nov-18");
      System.out.println("Date = "+dt);
   }
}

Output

Date = Tue Nov 20 00:00:00 UTC 2018
raja
Published on 17-Dec-2018 12:49:43
Advertisements