I have a string 10-MAR-1998 14:47
which I need to convert to DateTime object of a specified format. For example: (dd-MMM-yyyy HH:mm GMT) in java.
Asked
Active
Viewed 4,438 times
0
-
Your question is unclear - when you say "DateTime object", do you mean a joda DateTime? If not what class are you referring to? Also a date/time object generally represents a point in time but doesn't have a format - you format it when you need to show it to a user using a formatter. – assylias Apr 04 '16 at 09:04
-
There might be about thousands of java date format questions out there in SO. I cannot imagine, you didn't find anything. – mad_manny Apr 04 '16 at 09:08
-
check this link :http://stackoverflow.com/questions/6252678/converting-a-date-string-to-a-datetime-object-using-joda-time-library – soorapadman Apr 04 '16 at 09:10
2 Answers
0
DateTimeFormatter
and
SimpleDateFormat
are your friends, my friend. The only question is, is the date time of the input string also in GMT?
If it is, then:
String myString = "10-MAR-1998 14:47";
DateFormat format = new SimpleDateFormat("d-M-y H:m");
Date date = format.parse(myString);
should do the trick ;)

Reto
- 1,305
- 1
- 18
- 32
0
You could try SimpleDateFormat
In your case you would have
String str = "10-MAR-1998 14:47";
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy hh:mm");
Date date = df.parse(str);
You can also go back from date to string with the specified format like this :
String dateStr = df.format(df.parse(date));
I know that the library Joda Time
handles quite well date conversion. But you would have to add a dependency to your project and if it's the only thing you need for date conversion right now I would suggest sticking with SimpleDateFormat.

madube94
- 175
- 2
- 12