Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String in Date

 
Rafael Roque
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
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
Pie
Posts: 65225
95
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
Pie
Posts: 65225
95
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
Pie
Posts: 65225
95
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
 
Scott Selikoff
author
Saloon Keeper
Posts: 4028
18
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic