How to pass dates in Java to a database?

Asked

Viewed 549 times

3

I’m trying to pass a date in format "yyyy/MM/dd HH:mm:ss" to the database for this I am using the Date() and the SimpleDateFormat(), but he returns me one string. Is there any way he can return me a date or if there is any conversion for such?

  • It has a knack but depends on what you need. Maybe your problem is not just using the Date(). Have want to see what your database is waiting for. You did not give details of how you are doing, did not put code. SimpleDateFormat() is for mounting strings even and not to format dates, because dates do not have format, dates are dates. Only texts that show dates have format. On a date what matters is the value contained in it and not the way it is presented.

  • @bigown Thanks for the tip guy, I was looking at the bank that is from a project made by third parties and he expected datetime, so I solved the problem just by changing from Date() to Timestamp().

  • I will reply not to leave unanswered.

1 answer

2


It has a knack but depends on what you need. It has to see what your database is waiting for. It is sending the data in a different type than expected by the database. So you need to find out what is the expected type and send this way.

SimpleDateFormat() is for mounting strings even and not to format dates, because dates do not have format, dates are dates. Only texts that show dates have format. On a date what matters is the value contained in it and not the way it is presented.

In fact the author discovered that he needed to use Timestamp().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.