File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes DateFormat problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "DateFormat problem" Watch "DateFormat problem" New topic
Author

DateFormat problem

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
In K&B page 465
I ran the program

The output is
d1 = Sun Sep 09 07:16:40 GMT+05:30 2001
9/9/01
parsed = Sun Sep 09 00:00:00 GMT+05:30 2001


K&B says
Notice that because we were using a SHORT style, we lost some precision when we converted the Date to a String. This loss of precision showed up when we converted back to a Date object, and it went from being 7:46 to midnight.

I changes the style to DateFormat.LONG or even MEDIUM,FULL still gets the same output.Still there is a precision loss?


~Sanjeev Singh<br />SCJP 1.5
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
Sanjeev

The problem is that you are calling:



getDateInstance returns a formatter that will only format the date and not the time. So changing the style has no effect on the time part since it is ignored.

Regards

Dave
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
David,
This means that the description in the K&B book is not proper.It is showing the reason as "since the formater is using DateFormat.SHORT so there will be a precision loss."
What should we do in order to format the time also?
[ January 03, 2007: Message edited by: Sanjeev Kumar Singh ]
David Grindley
Greenhorn

Joined: Oct 30, 2006
Posts: 14
Sanjeev

I agree with you, it looks like K & B has an error. Anyhow if you want to get a date time format use:


You can find 3 overridden versions of this method in the API documentation.

Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DateFormat problem
 
Similar Threads
Updated K&B 5 Errata - 4/16
doubt in parsing
parse( ) of DateFormat
Date Format related
problem with dates