aspose file tools*
The moose likes Java in General and the fly likes Convert String in Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert String in Date" Watch "Convert String in Date" New topic
Author

Convert String in Date

Rafael Roque
Greenhorn

Joined: Sep 21, 2006
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61066
    
  66

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Also, please reserve this forum for advanced Java questions.

Moved to the intermediate forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Have you checked out the parse() method of DateFormat?
Rafael Roque
Greenhorn

Joined: Sep 21, 2006
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61066
    
  66

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

Joined: Sep 21, 2006
Posts: 3
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

"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


My Blog: Down Home Country Coding with Scott Selikoff
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16


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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert String in Date