-2

How to convert this String

Sat May 07 04:31:14 MST 2016

into datetime object.

BigBang
  • 149
  • 12

3 Answers3

2

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.

user1438038
  • 5,821
  • 6
  • 60
  • 94
  • how to convert date object to datetime because simple date format returns date object but i need datetime object – BigBang Dec 03 '14 at 13:40
  • What DateTime class are you using (full name with package)? If it is ``org.eclipse.swt.widgets.DateTime``, you can simply do: ``dateTimeObj.setDate(dateObj);`` – user1438038 Dec 03 '14 at 14:10
  • I am using joda datetime – BigBang Dec 04 '14 at 05:50
  • You can use the ``parse(String)`` method then: http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#parse(java.lang.String) – user1438038 Dec 04 '14 at 07:31
1

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);
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
1

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

Community
  • 1
  • 1
MSA
  • 2,502
  • 2
  • 22
  • 35