IT Certificaions

Google Cloud Platform

Monday, July 11, 2011

Convert String to date in java

 Convert String(yyyy-MM-dd HH:mm:ss ) to date in java
---------------------------------------------------------------------------
public static Date getCurrentTimeStamp(){
        String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        DateFormat formatter= new SimpleDateFormat(DATE_FORMAT);
        Calendar c1 = Calendar.getInstance(); // today
      
        System.out.println("Today is " + formatter.format(c1.getTime()));
       
        try {
           
            Date date = (Date)formatter.parse(formatter.format(c1.getTime()));
             System.out.println("Date after converted from string: " + date);
             return date;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
      
      }