I had a String in this form, EEE, dd MMM yy hh:mm:ss Z and I wanted to convert it into the Date object so i did this,
SimpleDateFormat fDate = new SimpleDateFormat("EEE, dd MMM yy hh:mm:ss Z",Locale.getDefault());
then I created the instance field of type Date
Date toDate = null;
and parse my String date and store it into toDate
toDate = fDate.parse(stringDate);
Now the date is stored in toDate variable in this form,
Tue Jan 20 07:33:06 GMT+05:30 2015
but this is not what i wanted.
I wanted my final Date object in this form,
Tue Jan 20, 2015
So, in order to achieve this, I created the new Calendar object and set its time to toDate,
Calendar s = Calendar.getInstance();
s.setTime(toDate);
then I create a new String and store Day of Week, Month Day of Month and year to get this format Tue Jan 20, 2015.
String newDate = s.getDisplayName(Calendar.DAY_OF_WEEK,0,Locale.getDefault()) + " "
+ s.getDisplayName(Calendar.MONTH, 0, Locale.getDefault())
+ " " + s.get(Calendar.DAY_OF_MONTH) + ","
+ s.get(Calendar.YEAR);
Now, my new string newDate has a date in this form Tue Jan 20, 2015 and the problem is it is a String object and not a Date object, and in order to convert it to Date object I have to use the SimpleDateFormatter again.
But this is a lot of work because in order to convert String of this form, EEE, dd MMM yy hh:mm:ss Z into the Date object of this form, Tue Jan 20, 2015, I am first using SimpleDateFormatter then I am parsing it in a Date object, then I extract my desire fields from that Date object using Calendar and store in a String, and then convert that String back to the Date object.
Is there any easier way to achieve this ? i.e, converting this String EEE, dd MMM yy hh:mm:ss Z directly into this format Tue Jan 20, 2015 of type Date without using SimpleDateFormatter twice ?