aspose file tools*
The moose likes Beginning Java and the fly likes Doubt in SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in SimpleDateFormat" Watch "Doubt in SimpleDateFormat" New topic
Author

Doubt in SimpleDateFormat

vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Hi

Here is the requirement "how to convert a string into date without changing its format"
When i tried to do this using SimpleDateFormat :
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(str);

Output
String 26/08/1994 gets parsed as Fri Aug 26 00:00:00 IST 1994.
The format gets changed is it possible to preserve the format?

Thanks in advance
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

"vinodh rm", Welcome to the Ranch. Please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

A Date object has no notion of formatting. Sure you can print it, but that is only informational. All a Date object is is a representation of a moment in time.
You should always use DateFormat and its subclasses to both parse and format Date objects.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Thanks for the reply, apologize for the confusion, let me refine the question.
I want to know if its possible to display the date object in the same way as the String which contains the date, say for ex:
Consider the string 26/08/1994 is it possible to display a date object in this fashion?
A sample code will be very helpful, thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Sure, just reuse your formatter object:

This may require that you'll need to retain a reference to the (Simple)DateFormat object (as a private field) if you need it in multiple methods.
vinodh rmahen
Greenhorn

Joined: Jul 22, 2008
Posts: 14
Thanks for clarifying.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in SimpleDateFormat