1

I am trying to store date in sql server 2005 i am used "datetime" data type in sql server and in java program i am passing string as date

ex.

String DateStr = "12/12/2013";

Date d = new Date();
java.sql.Date d1 = new java.sql.Date(d.getTime());

String sql = "INSERT INTO info (date) VALUES ( ? )";

try {
      pstmt = con.prepareStatement(sql);
      pstmt.setDate(1, d1);
      pstmt.executeUpdate();
     } catch (SQLException e) {
            System.out.println("Error:" + e);
     }

the above code is working...but i want to use String DateStr = "12/12/2013"; insted of d.getTime() bcoz i passed date as string to java function by using jquery datepicker

3 Answers 3

7

You need to use a SimpleDateFormat to parse your String to a date and then use that date.

Date d = new SimpleDateFormat("dd/MM/yyyy").parse(DateStr); // This throws a ParseException

// Rest everything stays pretty much the same
java.sql.Date d1 = new java.sql.Date(d.getTime());
...
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry For too late....It's store the current date like (thursday 28-11-2013) not DateStr
the DateStr is the String holding your date. what format does it have?
in sql server 2005 i am using datetime as my datatype and want to store String DateStr="25/11/2013"; such string into datetime but failed
0
public long convertLong(String value) {
    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date getDate = null;
    long returnDate = 0l;
    try {
        getDate = (Date) formatter.parse(value);
        returnDate = getDate.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return returnDate;
}

Input: 12/12/2013 Output: 1386786600000

3 Comments

and how to use it to store in database
Like you are doing java.sql.Date d1 = new java.sql.Date(d.getTime()); I'm not sure but will work. I just gave you solution for getting d.getTime() from your dd/MM/yyyy
Have you tried like java.sql.Date d1 = new java.sql.Date(convertLong("12/12/2013")) ?
0

First convert string in datetime object than you can directly pass d1 to your code

String DateStr = "12/12/2013";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy ");
DateTime dt = formatter.parseDateTime(DateStr);
String sql = "INSERT INTO info (date) VALUES ( dt )";
try {
      pstmt = con.prepareStatement(sql);
      pstmt.setDate(1, d1);
      pstmt.executeUpdate();
     } 
catch (SQLException e) {
            System.out.println("Error:" + e);
     }

2 Comments

Thanx for replying but it shows 'java.lang.IllegalArgumentException: Invalid format: "28/11/2013" is too short'
and if i use pstmt.setDate(1, dt); it shows 'DateTime CanNot be converted to Date'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.