Get the current date in java.sql.Date format

后端 未结 10 1796
傲寒
傲寒 2021-02-03 17:14

I need to add the current date into a prepared statement of a JDBC call. I need to add the date in a format like yyyy/MM/dd.

I\'ve try with

         


        
相关标签:
10条回答
  • 2021-02-03 17:50

    These are all too long.

    Just use:

    new Date(System.currentTimeMillis())
    
    0 讨论(0)
  • 2021-02-03 17:50

    all you have to do is this

        Calendar currenttime = Calendar.getInstance();               //creates the Calendar object of the current time
        Date sqldate = new Date((currenttime.getTime()).getTime());  //creates the sql Date of the above created object
        pstm.setDate(6, (java.sql.Date) date);              //assign it to the prepared statement (pstm in this case)
    
    0 讨论(0)
  • 2021-02-03 17:56

    You can achieve you goal with below ways :-

    long millis=System.currentTimeMillis();  
    java.sql.Date date=new java.sql.Date(millis);  
    

    or

    // create a java calendar instance
    Calendar calendar = Calendar.getInstance();
    
    // get a java date (java.util.Date) from the Calendar instance.
    // this java date will represent the current date, or "now".
    java.util.Date currentDate = calendar.getTime();
    
    // now, create a java.sql.Date from the java.util.Date
    java.sql.Date date = new java.sql.Date(currentDate.getTime());
    
    0 讨论(0)
  • 2021-02-03 17:58

    Since the java.sql.Date has a constructor that takes 'long time' and java.util.Date has a method that returns 'long time', I just pass the returned 'long time' to the java.sql.Date to create the date.

    java.util.Date date = new java.util.Date();
    java.sql.Date sqlDate = new Date(date.getTime());
    
    0 讨论(0)
提交回复
热议问题