Inserting value in SQL Server’s datetime by java.sql.Timstamp
Posted on 08. Apr, 2007 by Tahir Akram in Development
public static Timestamp getTodayTimestamp ()
{
java.util.Date date = null;
java.sql.Timestamp timeStamp = null;
try
{
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
java.sql.Date dt = new java.sql.Date(calendar.getTimeInMillis());
java.sql.Time sqlTime=new java.sql.Time(calendar.getTime().getTime());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
date = simpleDateFormat.parse(dt.toString()+" "+sqlTime.toString());
timeStamp = new java.sql.Timestamp(date.getTime());
}
catch (ParseException pe)
{
}
catch (Exception e)
{
}
return timeStamp;
}



bobby1234
12. May, 2007
Hi
that works a treat… thanks very much…
dates are a pain … and dates between java and sql are worse…
this code did the business for me thanks
marshall
20. Nov, 2007
Uh….according to the javadoc for java.sql.Timestamp the ToString function returns a String object in yyyy-mm-dd hh:mm:ss.fffffffff format.
So the sqlTime.toString() function should be sufficient, it works for me with mysql.
marshall
20. Nov, 2007
Silly me, I see now that you are using the java.sql.Time and not Timestamp. I’m so used to seeing Timestamp I just glossed over that part. Good example of how to use the SimpleDateFormat. I never did understand how to use it just by looking at the javadoc.
Danny Arica
30. Oct, 2008
Muy buen Dato estimado Amigo, estaba teniendo problemas, por que necesitaba conocer fecha y hora de transaccion.
Gracias
nitin
22. Dec, 2008
I think this code should be enough…
public static Timestamp getTodayTimestamp ()
{
Calendar calendar=Calendar.getInstance();
Timestamp timeStamp = new Timestamp(calendar.getTimeInMillis());
return timeStamp;
}
Tahir Akram
22. Dec, 2008
Hi nitin;
Thanks for your comments.