Java date to sql date

前端 未结 2 1905
無奈伤痛
無奈伤痛 2020-12-12 06:31

How can I store a specific date from java to my database? (Not only the date today, but also some dates that the user wants to specifiy)

try {
        Simpl         


        
相关标签:
2条回答
  • 2020-12-12 06:59

    Use

    prs.setDate( 1, new java.sql.Date( date.getTime() ) );
    

    The types java.sql.Date and java.sql.Timestamp are suppoed to be used to set date and timestamp fields, respectively. Read their documentation for the differences between them and between them and java.util.Date.

    You usually get a java date (java.util.Date) from the user. You convert it to milliseconds since the epoch using getTime(), and then convert back to a java.sql.Date or java.sql.Timestamp as I have shown.

    0 讨论(0)
  • 2020-12-12 07:02

    use this it can help

      static  java.sql.Timestamp getSqlDate(Date date) {    
            java.util.Date javaDate = date;
            long javaTime = javaDate.getTime();
            java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(javaTime);
            return sqlTimestamp;
     }
    
    0 讨论(0)
提交回复
热议问题