How to convert this String
Sat May 07 04:31:14 MST 2016
into datetime object.
This code will do:
SimpleDateFormat formatter = new SimpleDateFormat(/** TODO: Enter your pattern here */);
String dateInString = "Sat May 07 04:31:14 MST 2016";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
}
catch (ParseException e) {
e.printStackTrace();
}
For a full reference of the modifiers that are allowed in the date pattern, have a look at this web site.
Try following code:
String date="Sat May 07 04:31:14 MST 2016";
Date dateTime=new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy").parse(date);
System.out.println(dateTime);
You can use two metods
DateFormat
OR
SimpleDateFormat
Example:
String str_date="11-June-07";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date date = formatter.parse(str_date);
Source: stackoverflow
Other Examples"
public static void main(String[] args)
{
String expectedPattern = "MM/dd/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(expectedPattern);
try
{
String userInput = "E MMM dd HH:mm:ss zzz yyyy";
Date date = formatter.parse(userInput);
System.out.println(date);
}
catch (ParseException e)
{
e.printStackTrace();
}
}
Source: alvinalexander