1

Hello i'm defining a variable in which i want to stock date-time input.
I gave this variable a format which is yyyy-mm-dd hh:MM. But in the database it keeps showing me only this format yyyy-mm-dd without hh:MM
The code

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd hh:MM");
                  java.util.Date date = sdf1.parse(rs.getString("dateMaint"));
                  java.sql.Date sqlStartDate = new Date(date.getTime()); 
                  mc.setDateMaint(sqlStartDate );
4
  • Little m stands for minutes while M stands for months... You'll have to switch them in your code Commented Mar 3, 2016 at 22:15
  • 1
    Also, a java.sql.Date doesn't have a time portion. So that's not really surprising. Use a Timestamp. And choose the appropriate type for the database column, of course. Commented Mar 3, 2016 at 22:18
  • Thank you so much for replying. Do you suggest anything using Timestamp ? Commented Mar 3, 2016 at 22:26
  • @user3816341 Please search Stack Overflow before posting. This has been covered many times already. Commented Mar 5, 2016 at 19:03

2 Answers 2

0

Also check if the database data type for your data is correct. This is dabase vendor dependant, for example an Oracle date type contains both time and date, but MS SQL server date means only date part: https://msdn.microsoft.com/en-us/library/bb630352.aspx

Sign up to request clarification or add additional context in comments.

Comments

0
public static java.sql.Timestamp convertToSqlDateTime(Date utilDate){
        return new java.sql.Timestamp(utilDate.getTime());
}

Normally, java.sql.Date only returns a date-only value and time will be discarded. So, in order to get time also, java.sql.TimeStamp must be used.

TimeStamp constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.

For this purpose, utilDate.getTime() is used to return the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date Object.

For more information link: Source

3 Comments

java.sql.date does handle up to milliseconds of precision, see: docs.oracle.com/javase/8/docs/api/java/sql/Date.html However you are right that the implementation of date, time, datetime differs accross database vendors.
@GeeBee Read the javadoc: To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
Javadoc also says: public void setTime(long date) Sets an existing Date object using the given milliseconds time value. If the given milliseconds value contains time information, the driver will set the time components to the time in the default time zone (the time zone of the Java virtual machine running the application) that corresponds to zero GMT.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.