2

i need to format this String date Tue Sep 23 14:36:59 PKT 2014 to java sql date but i need both date and time, i tried this

String st = "Tue Sep 23 14:36:59 PKT 2014";
new java.sql.Date(new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse(st).getTime())

but it returns only date part and time part is 00:00 can any one help me to sort this issue out.

Regards and thanks in advance.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
  • Run this. http://ideone.com/BC5w3u – Ruchira Gayan Ranaweera Sep 23 '14 at 09:58
  • Possible duplicate http://stackoverflow.com/questions/13982870/string-java-util-date-java-sql-date-with-time-stamp –  Sep 23 '14 at 10:04
  • System.out.println(new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse(st).toLocaleString()); System.out.println(new Date(new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse(st).getTime()).getTime()); System.out.println(new Date(new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy").parse(st).getTime()).toLocaleString()); – Raghunandan Krishnamurthy Sep 23 '14 at 10:09

4 Answers4

4

java.sql.Date doesn't contain time information. You should look at java.sql.Timestamp instead

Joel
  • 2,374
  • 17
  • 26
0

As its name suggests, java.sql.Date is the Java equivalent of an SQL DATE meaning it stores years, months and days only -- no time data (as a result, note also that java.sql.Date isn't associated with a time zone).

Ben
  • 7,548
  • 31
  • 45
0

try this:

java.sql.Timestamp ourJavaTimestampObject = new java.sql.Timestamp(dateTime.getTime().getTime());

with dateTime is an instance of Date. It works to me.

Phong Nguyen
  • 277
  • 4
  • 16
0

see this link

http://www.coderanch.com/t/304851/JDBC/databases/Java-date-MySQL-date-conversion

The following lines may solve your issue.

java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
benka
  • 4,732
  • 35
  • 47
  • 58
guest
  • 1