• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Convert String in Date

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
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
"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
Sheriff
Posts: 67753
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
Also, please reserve this forum for advanced Java questions.

Moved to the intermediate forum.
 
Bear Bibeault
Sheriff
Posts: 67753
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
Have you checked out the parse() method of DateFormat?
 
Rafael Roque
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4354
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic