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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to Convert String into Date" Watch "How to Convert String into Date" New topic
Author

How to Convert String into Date

Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello Friends,
I have a string in date format that is "2000-11-05 13:34:56" now i want to convet it into Date object.
I found following methods which allows me to convert String object in to Date but they all are depricated and i am not able to use tem.
1. java.util.Date constructor
Date(String)
2. public static long parse(String)
3. public static java.sql.Date valueOf(string)
So i am not able to uses these depricated methods and can any one pls help me how to do this.
Second, about depricated methods. What i know is that we can use depricated methods in our source code and it will compile with warnings. Ok this this is happening, my source code is converted into bytecode with 1 warning. Now when i run the class file it execute all the commands till it reached the statement where depricated method is used. It then ignores all the lines after that and at the same time i don't get any error message on console.
In short i am not able to use any of the depricated methods. Can some one pls high light on this also.

thanks,
regards,
Raj.


Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
Preethi Suryam
Ranch Hand

Joined: Nov 17, 2000
Posts: 92
Hi Raj!
Date(String s) is replaced by DateFormat.parse(String s)
It is working fine.try it out.
Good Luck
Preethi.
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello ,
I tried what you suggested. DateFormat is an abstract class. SimpleDateFormat is its concrete subclass. So i created object of SimpleDateFormat class and using that object i accessed parse method of DateFormat class.

SimpleDateFormat sdf = new SimpleDateFormat();
java.util.Date date = sdf.DateFormat("2000-11-04");

I also passed string in this format:
java.util.Date date = sdf.DateFormat("2000-11-04 14:25:56");
for both the cases it gave me the following error
java.text.ParseException unparse Date : "2000-11-04"
or
java.text.ParseException : "2000-11-04 14:25:56"
Preeti can you test this thing? I am really desparate and i need a solution for this. May be i am doing something wrong. In that case pls tell me where i am going wrong. Second if you have tried this then tell me how to do it with a piece of code.
Thanks regards,
Raj.
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello ,
I tried what you suggested. DateFormat is an abstract class. SimpleDateFormat is its concrete subclass. So i created object of SimpleDateFormat class and using that object i accessed parse method of DateFormat class.

SimpleDateFormat sdf = new SimpleDateFormat();
java.util.Date date = sdf.DateFormat("2000-11-04");

I also passed string in this format:
java.util.Date date = sdf.DateFormat("2000-11-04 14:25:56");
for both the cases it gave me the following error
java.text.ParseException unparse Date : "2000-11-04"
or
java.text.ParseException : "2000-11-04 14:25:56"
Preeti can you test this thing? I am really desparate and i need a solution for this. May be i am doing something wrong. In that case pls tell me where i am going wrong. Second if you have tried this then tell me how to do it with a piece of code.
Thanks regards,
Raj.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
The way to do this is:

Hope this helps


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rajpal Kandhari
Ranch Hand

Joined: Aug 26, 2000
Posts: 126
Hello Carl,
Thanks, for you're help. It worked out and finally i am able to convert string into date.
Thanks once again.
Regards,
Raj.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to Convert String into Date