2

Please advise how can I convert the date that I am getting in String form to java.sql.Date form..

String Dateimp = abcObject.getabcDate();
java.sql.Date  sd = ?????  // what should i write in here.

at last finally i want to convert the date that I am getting in String form to java.sql.Date

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
user3440926
  • 79
  • 1
  • 2
  • 9
  • I'd be the same process as converting a `String` to a `java.util.Date`... – MadProgrammer Mar 21 '14 at 05:21
  • @MadProgrammer Can you please post the code so that I can grasp – user3440926 Mar 21 '14 at 05:24
  • Search "Java String to Date" - there are literally thousands of examples. The question will be, what format is `Dateimp` in... – MadProgrammer Mar 21 '14 at 05:30
  • See also: http://stackoverflow.com/questions/4216745/java-string-to-date-conversion?rq=1 combined with http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date?rq=1 – Jason C Mar 21 '14 at 05:31
  • FYI, the troublesome old date-time classes such as `java.sql.Date` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 14 '17 at 01:33

4 Answers4

4

You need to know the exact format of your string, for intance if your date is formatted as "2014-01-30",

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = df.parse(Dateimp);
// if you really need java.sql.Date
java.sql.Date sqlDate = new java.sql.Date(date.getTime());

Read how to build the date pattern here http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

huocp
  • 3,898
  • 1
  • 17
  • 29
3

Try this

    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    Date parsedate = format.parse(YOUR STRING DATE);
    java.sql.Date sql = new java.sql.Date(parsedate.getTime());

Last line will convert your Date object to SQL Date.

Abiel Paltao
  • 315
  • 2
  • 12
2

//This convert the string to Date

  SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";

try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

} catch (ParseException e) {
    e.printStackTrace();
}

/* Below code snippet convert a java util Date into a sql Date for use in databases */

java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
loknath
  • 1,362
  • 16
  • 25
1

You can directly retrieve it some thing like

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
java.sql.Date sql;
java.util.Date date;

try {
    Date today = df.parse(rs.getString("date"));
    date= new java.util.Date();
      sql= new java.sql.Date(date.getTime());

} catch (ParseException e) {
   e.printStackTrace();
}

The date in "sql" can use for the db conversations

Gowthami Reddy
  • 430
  • 7
  • 23