Friday 21 April 2017

Java program to parse String to Date format


package DateFormat;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class StringToDate {
 
       public static void main(String[] argsthrows ParseException {
              String dateString = "21/4/2017";
             
              SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
              //this step throws checked exception if main() doesn't throws ParseException
              Date date = sdf.parse(dateString);   
             
              System.out.println("Date:"" " + date);
 
       }
 


OUTPUT

Date: Sat Jan 21 00:04:00 IST 2017
 

1 comment:

  1. Month should be April instead of Jan in the output.

    ReplyDelete

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.