STRING TO DATE CONVERSION IN JAVA
package basic;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String[] args) {
try {
// STEP 1-take date as a string
String date_is = "29/06/20016";
// STEP 2-creates objects creates object of
// SimpleDateFormat(java.text.SimpleDateFormat)
// and pass the date format into its constructor
SimpleDateFormat strformat = new SimpleDateFormat("dd/MM/yyyy");
// STEP 3- use parse() method of SimpleDateFormat and put string
// into its constructor
Date date = strformat.parse(date_is);
System.out.println("String to Date: " + date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
output-->
String to Date: Wed Jun 29 00:00:00 IST 20016
No comments:
Post a Comment