I am unable to get a date instance in "EEE MMM dd HH:mm:ss.SSS zzz" yyyy format.
I am getting it in String format but when I am trying to parse it to Date format , I am not getting the required.I am missing the milisecond pattern.
Date objects do not have a format by themselves. So you cannot "get a Date instance in a particular format". A Date object is nothing more than a number of milliseconds since 1 January 1970, 00:00:00 GMT.
When you display a Date by calling toString() on it (explicitly or implicitly) as you are doing in line 9 of your code, then it will use some default format to display the date. The Date object does not remember how you created it, by parsing a string with a certain format.
If you want to display a Date with a specific format, you should use DateFormat.format(...) to convert it to a String.
You are printing a Date. If you'd print the "String value" instead, you'd see that the milliseconds are not lost. Date do not hold any format, so when you call System.out.println on a Date, Date#toString will be called. If you look at Date#toString API, it says :
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
01. Create a DateFormat object with the pattern "EEE MMM dd HH:mm:ss.SSS zzz yyyy"
02. Create a Calendar
03. Set the Calendar to the current date and time (not necessary because it is already set to the current date and time)
05. Format the current date and time of the Calendar to a String with the format "EEE MMM dd HH:mm:ss.SSS zzz yyyy"
08. Parse the string back to a Date object (why? you could just as well have done Date date_set = Calendar.getTime())
09. Print the Date object by calling toString() on it implicitly
You seem to expect that the Date object you create in line 8 remembers the format. It doesn't. Date objects do not know anything about formats. When you print the Date object in line 9, some fixed, default format will be used. Not the format "EEE MMM dd HH:mm:ss.SSS zzz yyyy" because the Date object doesn't know that it was created using that format string.
The fixed, default format doesn't include the milliseconds. If you need to display the milliseconds, then you'll need to format the Date object yourself using an appropriate DateFormat object, instead of calling toString() (explicitly or implicitly) on the Date object.
buntha Choudhary wrote:I want it in Date.
If you expect the Date object to be able to format itself displaying milliseconds, then that's not possible, as we're trying to explain to you.
Hello everyone...thanks for discussin and answerin...
i wanted to pass a date object in a particular format to a method...like "yyyymmdd"...searchin the forum got this thread and so can i conclude that date object cannot be created in a particaluar format...?? need to pass it to a third party method, who is expectin date in this format..(yyyymmdd)..please note i dont want a String data type...i want it to be in a date data type.
As already mentioned multiple times, a Date object doesn't know anything about date formats. It's not possible to have a Date object in "yyyymmdd" format - there's no such thing as a Date object with a format.