0

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 ?

bhuvesh
  • 729
  • 1
  • 9
  • 21
  • 5
    A `Date` does not have a format. A `Date` is a temporal concept. A _format_ is something you apply to understand or express that concept. – Sotirios Delimanolis Jan 20 '15 at 17:23
  • so you mean a `Date` object can never equal to any custom format ? and it will always equal to the complete date including time, day, year, second, locale etc ? – bhuvesh Jan 20 '15 at 17:26
  • A `Date` represents a timestamp. The day, year, second, etc. is human readable information. To get that information you need to `format` the `Date`. It's not part of the `Date`. – Sotirios Delimanolis Jan 20 '15 at 17:54
  • possible duplicate of [Format date in java](http://stackoverflow.com/questions/4772425/format-date-in-java) – Basil Bourque Jan 21 '15 at 10:06
  • Please search StackOverflow before posting. You would find hundreds of Questions and Answers on this topic. – Basil Bourque Jan 21 '15 at 10:07
  • @BasilBourque, actually my question is different from what is suggested as duplicate...because I am already doing what is posted as an answer in the question http://stackoverflow.com/questions/4772425/format-date-in-java..this is non-sense. – bhuvesh Jan 22 '15 at 08:51

1 Answers1

1

While the Java Date library is notoriously bad (which is pretty much why everyone uses the Joda-Time library, and why Oracle released the new java.time package with Java 8 (see article), there are a couple things to be aware of:

  1. ALL java.util.Date represents is the number of milliseconds since midnight, Jan 1, 1970 in UTC. In other words, java.util.Date is really just a small wrapper around a long value.
  2. Thus, java.util.Date does NOT store any kind of formatting info. Date.toString() just always produces a String in a human readable "debug" format.
  3. To answer your question, no, there is really not a way to prevent the use of SimpleDateFormatter twice, but I don't see why you would want to avoid this, besides that it seems like you are misunderstanding the concepts of what a Date represents. If you have a java.lang.String and want to convert it to a java.util.Date, you use DateFormat.parse(), and if you have a java.util.Date and want to convert it to a java.lang.String, you use DateFormat.format().
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
adevine
  • 1,074
  • 9
  • 23