Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Convert String into Date

 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preethi Suryam
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way to do this is:

Hope this helps
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic