• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Doubt in SimpleDateFormat

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vinodh rm", Welcome to the Ranch. Please check your private messages for an important administrative matter.
 
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vinodh rmahen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic