wood burning stoves 2.0*
The moose likes Java in General and the fly likes conversion of string into java.sql.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 "conversion of string into java.sql.Date" Watch "conversion of string into java.sql.Date" New topic
Author

conversion of string into java.sql.Date

vikassheel gupta
Ranch Hand

Joined: Aug 02, 2005
Posts: 53
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


Warm Regards<br />vikassheelgupta<br />9911005168
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55

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.


Ping & DNS - my free Android networking tools app
vikassheel gupta
Ranch Hand

Joined: Aug 02, 2005
Posts: 53
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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

Joined: Aug 02, 2005
Posts: 53
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55

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

Joined: Aug 02, 2005
Posts: 53
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
Carolina Saborio
Greenhorn

Joined: Jan 06, 2010
Posts: 1
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..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
 
subject: conversion of string into java.sql.Date