Parse string date value input in Java


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements