i want to convert String "yyyy-MM-dd'T'HH:mm" (for ex,2015-04-13T10:00:00)
into date object with the same format(yyyy-MM-dd'T'HH:mm)
. Please let me know how to do this.
Asked
Active
Viewed 1,436 times
0
-
2Date objects don't *have* a format. You can parse using a particular format, but then when you want to convert the `Date` back into a `String`, you need to format with the same format. – Jon Skeet Sep 03 '15 at 12:07
1 Answers
1
String dateString = "2015-04-13T10:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
Date date = simpleDateFormat.parse(dateString);
Now if you want to display the date in the same format, you can do the following
System.out.println(simpleDateFormat.format(date)); // Outputs "2015-04-13T10:00"

Kevin
- 2,813
- 3
- 20
- 30
-
-
You probably did the following `System.out.println(date.toString());` , @Jon Skeet comment explains why it is outputting in that format. – Kevin Sep 03 '15 at 12:10
-
@santro I edited my answer in order to show how to print the date like you want. – Kevin Sep 03 '15 at 12:16
-
-
i want to update this format(datetime) in mysql db. how to achieve this. – santro Sep 03 '15 at 12:28
-
@santro Note that this (new) question is in fact off topic here. However, this should help you out : http://stackoverflow.com/questions/15212832/how-to-save-current-date-and-time-to-database-using-java – Kevin Sep 03 '15 at 12:32
-