File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Unable to get  Date instance in a particular format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to get  Date instance in a particular format" Watch "Unable to get  Date instance in a particular format" New topic
Author

Unable to get Date instance in a particular format

buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I am missing the milisecond pattern.

Missing ? What do you mean by missing ? Can you give us an example of your output ?


[My Blog]
All roads lead to JavaRanch
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

sample code -



And the output I am getting as -
date_set Wed Jul 21 12:07:43 IST 2010
value Wed Jul 21 12:07:43.113 IST 2010


I think I am doing something wrong. So please provide me the correct way to achieve the data instance as Wed Jul 21 12:07:43.113 IST 2010
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

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.



Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

I tried that but I want to return the value in Date format only.
I tried to convert the String to Date and thereby I was not able to get the milisecond.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

That's why you don't see any milliseconds.
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

Is there any possible way to achieve the required output only in date format?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You don't seem to understand the main concept. Date instances do not have any format. To prove you that the millisecs are not lost in the Date instance, try this after your System.out.println :


Do you want the date to be a String, or a Date ?
buntha Choudhary
Ranch Hand

Joined: Jul 03, 2009
Posts: 136

I want it in Date.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Your program is doing this:

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.
Naveen Kumar Kumar
Greenhorn

Joined: Jul 28, 2010
Posts: 19
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.

please correct me if my understanding is wrong..

thanks and cheers...


Push the limits to push yourself..!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

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.
Naveen Kumar Kumar
Greenhorn

Joined: Jul 28, 2010
Posts: 19
yep...thanks...!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to get Date instance in a particular format