Following code example shows how to format a String to a Date object. In following example the method convertStringToDate actually takes three arguments as integers, date, month and year, and then concatenates these values to a String date.
Code:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/
public class Main {
public void convertStringToDate(int d, int m, int y) {
SimpleDateFormat dF = new SimpleDateFormat("dd/MM/yyyy");
String in = d + "/" + m + "/" + y;
try {
Date theD1 = dF.parse(in);
System.out.println("Date parsed = " + dF.format(theD1));
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Main().convertStringToDate(20, 11, 2010);
}
}
Bookmarks