Java convert string to date java example
package com.w3spoint;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDate {
public static void main(String args[]){
try {
String dateString = "18-Mar-2018";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date date = df.parse(dateString);
System.out.println("Date: " + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
} |
package com.w3spoint; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; public class StringToDate {
public static void main(String args[]){
try {
String dateString = "18-Mar-2018";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date date = df.parse(dateString);
System.out.println("Date: " + date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output:
Date: Sun Mar 18 00:00:00 IST 2018 |
Date: Sun Mar 18 00:00:00 IST 2018
Download this example.