Sep 19, 2012

Java - Convert Staring to Date

Here's simple example how to do.
import java.util.*;
import java.text.*;
public class StringToDate {
  public static void main(String[] args) {
    try {
      String myDate="11-Sept-07";
      DateFormat formatter ; 
      Date date ; 
      // NOTE: myDate value must be same format so that 
      //     it will not get exception error
      formatter = new SimpleDateFormat("dd-MMM-yy");
      date = (Date)formatter.parse(str_date);  
      System.out.println("Today is " +date );
    } catch (ParseException e){
      System.out.println("Exception :"+e);
    }
  }
}

No comments: