Date Parsing using SimpleDateFormat

Java Programming Java8Object Oriented Programming

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
raja
Published on 25-Jul-2018 13:30:51
Advertisements