Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

How to Format a Date String using SimpleDateFormat?



Hi all,

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Test1 {

public static void main(String[] args) {

      SimpleDateFormat formatter;
      String output;

      formatter = new SimpleDateFormat("MM/dd/yyyy");
      Date d = null;
      try {
d = formatter.parse("12/20/08");//user entry date
} catch (ParseException e) {
e.printStackTrace();
}
      output = formatter.format(d.getTime());

      System.out.println(output);
  
}

}

The above code formats date string to the given format.
If user gives "12/20/2008" it prints 12/20/2008
If user gives "12/20/08" it prints 12/20/0008 but i need 12/20/2008 (year should be current year(2008) not 0008). How can I achieve this?

Thanks.
-arun