Sunday, 11 August 2013

Convert String to Date [Java]

API: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String str= "7-Jun-2013"; // same format
try {
   
            Date date = formatter.parse(str); // get Date instance


            System.out.println(date.getDate());
            System.out.println(date.getMonth()+1);
            System.out.println(date.getYear());
            System.out.println(date);

            System.out.println(formatter.format(date));
   
        } catch (ParseException e) {
            e.printStackTrace();
        }

No comments:

Post a Comment