I’d like to insert the value derived from this JXDatePicker into a Date field in Java DB. How should I get ONLY date off this controller in a way that time is represented as dd/mm/yyyy and nothing else??!
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can get the
Datefrom theJXDatePicker.getDate()and then use aSimpleDateFormatto format the date into the required format.Try:
Notice that instead of the
mmthat you’ve used I usedMMto represent the month. Please see theSimpleDateFormatjavadoc for pattern letters you can use.Follow-Up
I feel compelled to mention, for completeness, that it is generally a bad idea to put formatted strings representing dates into a database; what happens when you want to display it in another format, for instance, or do a simple comparison using
SQL.One way to store date/times is to use the timestamp that you get from
Date.getTime(). Here’s theDateclass’getTime()javadoc:Storing this representation of a
Datein your database makes it much simpler to create aDateobject when you retrieve the timestamp:Or use the column in
SQLto do a simple comparison:It also makes it somewhat portable so you can, for instance, send the timestamp to a thin client that is built using a different technology.
That being said, it is also in your best interest to use a date and time library like Joda Time instead of using the unreliable and inconvenient Java
DateorCalendarclasses.