Date Parsing using SimpleDateFormat


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

Example

Live Demo

import java.util.*;
import java.text.*;
 
public class DateDemo {
   public static void main(String args[]) {
      SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");        
      String input = args.length == 0 ? "1818-11-11" : args[0];  

      System.out.print(input + " Parses as ");        
      Date t;
      try {
         t = ft.parse(input);          
         System.out.println(t);        
      } catch (ParseException e) {          
         System.out.println("Unparseable using " + ft);        
      }
   }
}

A sample run of the above program would produce the following result −

Output

1818-11-11 Parses as Wed Nov 11 00:00:00 EST 1818

Updated on: 19-Jun-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements