• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Dates

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading a text file that has dates and would like to convert the string dates into a sql date that could be inserted into an Oracle 8.1.7 database. I've tried several iterations and have modified my stmt code to a prepared statement to help eliminate some of the syntax issues. The date coming in is in the format yymmdd and have converted this to yyyy-mm-dd. Could someone give me a complete code example of converting this string to a variable that can be used with the setdate stament in the prepared statement.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Welcome to JavaRanch!
Without using any deprecated methods or constructors (such as the very attractive constructor of the java.sql.Date class all of those attractive parts of the java.util.Date class), perhaps an easy route would be to construct a new java.util.Calendar object, parse your YYYY-MM-DD String for the numbers and use them to invoke Calendar::set( int , int , int ) . Then, get the date as a long with Calendar::getTimeInMillis() and finally construct a new java.sql.Date object with the constructor that takes a long.
Your JDBC driver should know what to do with a java.sql.Date object - you just have to tell it where to add the value.
Any luck?
 
John Gooch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give me an example in snipplites of code using the string sSolDate = YYYY-MM-DD. I've been searching forums left and right for examples and everyone I try does not work. here is the code I'm using now
DateFormat df = DateFormat.getDateInstance();
java.util.Date d = new java.util.Date();
try
{ d = df.parse(sSolDate);
}

catch(ParseException e)
{ System.out.println("Unable to parse " + sSolDate);
}

String sInsSol = "INSERT INTO ASFI_SOLICITATION " + "(SOLICITATION_NBR, AMMEND_NBR, RFQ_STATUS, CONTRACT_OFC_ID, SOLICITATION_DATE) " +
" VALUES (?, ?, ?, ?, ?)";

try
{ PreparedStatement pInsertSol = cSolCon.prepareStatement(sInsSol);
pInsertSol.setString(1, sSolNum);
pInsertSol.setString(2, sAmendNum);
pInsertSol.setString(3, sSolType );
pInsertSol.setString(4, sContractOfc );
pInsertSol.setDate(5, new java.sql.Date(d.gettime()) );

pInsertSol.executeUpdate();
}
which gives me the following error
Method gettime() not found in class java.util.Date.
 
John Gooch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give me an example in snipplites of code using the string sSolDate = YYYY-MM-DD. I've been searching forums left and right for examples and everyone I try does not work. here is the code I'm using now
DateFormat df = DateFormat.getDateInstance();
java.util.Date d = new java.util.Date();
try
{ d = df.parse(sSolDate);
}

catch(ParseException e)
{ System.out.println("Unable to parse " + sSolDate);
}

String sInsSol = "INSERT INTO ASFI_SOLICITATION " + "(SOLICITATION_NBR, AMMEND_NBR, RFQ_STATUS, CONTRACT_OFC_ID, SOLICITATION_DATE) " +
" VALUES (?, ?, ?, ?, ?)";

try
{ PreparedStatement pInsertSol = cSolCon.prepareStatement(sInsSol);
pInsertSol.setString(1, sSolNum);
pInsertSol.setString(2, sAmendNum);
pInsertSol.setString(3, sSolType );
pInsertSol.setString(4, sContractOfc );
pInsertSol.setDate(5, new java.sql.Date(d.gettime()) );

pInsertSol.executeUpdate();
}
which gives me the following error
Method gettime() not found in class java.util.Date.
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is very case sensitive, try this instead

Note: getTime() as opposed to gettime()
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

that should work. You did not specify the pattern to parse the date from.
Jamie
[ June 27, 2002: Message edited by: Jamie Robertson ]
 
John Gooch
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thansk to all for your help. Here is the code that finally worked: where sSolDate = "2002-02-05"
java.sql.Date d = java.sql.Date.valueOf(sSolDate);
String sInsSol = "INSERT INTO ASFI_SOLICITATION " + "(SOLICITATION_NBR, AMMEND_NBR, RFQ_STATUS, CONTRACT_OFC_ID, SOLICITATION_DATE) " +
" VALUES (?, ?, ?, ?, ? )";

try
{ PreparedStatement pInsertSol = cSolCon.prepareStatement(sInsSol);
pInsertSol.setString(1, sSolNum);
pInsertSol.setString(2, sAmendNum);
pInsertSol.setString(3, sSolType );
pInsertSol.setString(4, sContractOfc );
pInsertSol.setDate(5, d );

pInsertSol.executeUpdate();
}
catch (SQLException e)
{ System.err.println("Unable to create ASFI_SOLICITATION " + e);
System.exit(1);
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic