Convert String to Date - Java Date Time

Java examples for Date Time:Legacy Date

Description

Convert String to Date

Demo Code


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
        public static void main(String args[]){
                String strDate = "21/08/2011";
                try{
 
                        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                        Date date = sdf.parse(strDate);
                        System.out.println("Date is: " + date);
                }catch(ParseException e){
                        System.out.println("Java String could not be converted to Date: " + e);
                }//from  w  w w . j a v  a 2  s  .  c  o m
        }
}

Result


Related Tutorials