• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

conversion of string into java.sql.Date

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
i have a string in this format:-

String s = date+"-"+e_month+"-"+year;

and i want to covert it into

java.sql.Date sqlToday ;

so that i can save my values in the data base
i did every thing realted to that like:-

SimpleDateFormat ts= new SimpleDateFormat("yyyy-MM-dd");
sqlToday = new java.sql.Date(ts.parse(s).getTime());

but couldnt get the results
so any one can help me
thanks
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


but couldnt get the results



What does this mean? Any exceptions?

I notice that your string has the date first and year last, while the parse string is the other way round.
 
vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have change this also but
then problem is
i get
1-Aug-2005
and then if i try to convert this value in java.util.Date or java.sql.Date then the parsing error occurs
so
thats my problem
thanks
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use "MM" for the month it expects a numerical value; if the month is given as text, you need to use "MMM". Check the SimpleDateFormat javadocs - it explains this in more detail.
[ August 12, 2005: Message edited by: Ulf Dittmer ]
 
vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i used all this
now what i am doing is:-
String s = "1-Aug-2000";

SimpleDateFormat ts= new SimpleDateFormat("dd-MMM-yy");
if(p!=null)
{

sqlToday=new java.sql.Date(ts.parse(s).getTime());
}
System.out.println(sqlToday);
then the out put will be :-
2000-8-1
but didnt get the same result in the formate 1-Aug-2000
i need this value in the java.sql.Date format not in the string this time it is in string
thanks alot
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i need this value in the java.sql.Date format not in the string this time it is in string



What do you mean, you need it in java.sql.Date format? It is a java.sql.Date. If you want to print it in a specific format, check out the various SimpleDateFormat.format() methods. Printing it using System.out.println will call an objects toString() method, which knows nothing about your preferred format - it uses a default one.
 
vikassheel gupta
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
what i want to do is "1-Aug-1999" in a java.sql.Date formate this time i have this in string
so tell me how to do that i did all the ways from my side if u have any other then plsss help me thanks alot
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This worked for me..

String startDate format is "dd/mm/yyyy".

java.sql.Date theDate = new java.sql.Date(Integer.parseInt(startDate.substring(6, 10))- 1900, Integer.parseInt(startDate.substring(3, 5))-1,Integer.parseInt(startDate.substring(0, 2)));

it may not be the correct way to do it.. because it is deprecated... but at least it works..
 
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
Carolina, welcome to JavaRanch.

Please note that you are replying to a topic from 2005 - the original poster is most likely not still waiting for an answer.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic