Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Convert String in Date  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fellows,

I�m wondering how to convert a java.lang.String in a java.util.Date object,
but i�m not being succesful in doing this.

I�ve seen lots of examples in which the authors use the SimpleDateFormat class to format date�s presentation,but that�s not what I want.What I really want is to transform a String in a Date object,and I�d be very thankful if someone could help me in doing this

Regards,
Rafael Roque
 
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"xx xx",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please reserve this forum for advanced Java questions.

Moved to the intermediate forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked out the parse() method of DateFormat?
 
Rafael Roque
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, i�ve already checked it..

Let�s suppose that I use the DateFormat�s parse()method
as shown below:

String data = "24/07/1983";
SimpleDateFormat df = new SimpleDateFormat("dd/mm/yyyy");
Date d = df.parse(data);

The output will be:
Mon Jan 24 00:07:00 BRT 1983

But what I need is a "dd/MM/yy" output,and I�m wondering what should I do to achieve such result

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by xx xx:
The output will be:
Mon Jan 24 00:07:00 BRT 1983



The output has absiolutely nothing to do with parsing the input. A date value is a date value.

To format the output, use the format() method of a DateFormat implementation.
 
Rafael Roque
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I format my Date object using the DateFormat�s format()method,
I will end up obtaining a String,but all I need is simply to convert an object of one type(String) to another one(Date)

Thanks,
Rafael Roque
 
author
Bartender
Posts: 4096
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Rafael Roque"

Please re-read the naming policy in regards to obviously fictitious and choose a more appropriate name for the JavaRanch. This is your second warning.

-Scott
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I will end up obtaining a String,but all I need is simply to convert an object of one type(String) to another one(Date)



Your code does exactly that, convert a String to a Date object. What is it that you don't understand?

If you print the Date object after parsing it by just doing a System.out.println(d); then the Date will ofcourse be converted back to a string automatically, because it needs to be in a string format to be displayed on the screen. If you don't specify how the Date object should be formatted, then the toString() method of class Date is called automatically which uses a default format, so that you get:

Mon Jan 24 00:07:00 BRT 1983

Do you understand that a Date object is just a Date object - it doesn't have any implicit formatting.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!