Results 1 to 4 of 4
  1. #1
    habiler is offline Member
    Join Date
    Sep 2016
    Posts
    44
    Rep Power
    0

    Default System.out.print date format

    Hello,

    When I use rs2.getString(aDate) the result of this is 2017-01-07 13:08:35.000000 .
    But I would for result 07-Jan-2017. How I can do that and must the code be written?

    Thanks for all

    Habiler

    Java Code:
        private void jTxtMatrFocusLost(){
                    
                  PreparedStatement ps1,ps2;
                  Connection conn = Connect.ConnectDB();  
    
             
                try {  
       
                    String SQL1 = "SELECT * FROM Signaletique where Matricule = ?";
                    
                        
                        ps1 = conn.prepareStatement(SQL1); 
                        ps1.setString(1,jTxtMatr.getText());  
                        ResultSet rs1 = ps1.executeQuery();
                
                     String SQL2 = "SELECT * FROM Decisions where Matr = ?";   
                        
                        ps2 = conn.prepareStatement(SQL2); 
                        ps2.setString(1,jTxtMatr.getText());  
                        ResultSet rs2 = ps2.executeQuery();
    
                        affiche("Matr Sql2:"+jTxtMatr.getText()) ;
                        affiche("Matr Sql2:"+jTxtMatr.getText()) ;
                while ( rs1.next()) {
                               
                jTxtNomPrenom.setText(rs1.getString(3)+ " " + rs1.getString(4));
                jTxtMatr2.setText(rs1.getString(3)+ " - " + rs1.getString(4));
                
                while ( rs2.next()) {
    
    
                    
                    
                textArea2.append((rs2.getString(2)+"=>"+
                rs2.getString(3)+" - "+
                rs2.getString(4)+" - "+
                rs2.getString(5)+" - "+
                rs2.getString(6)+ "\n"));       
    System.out.print("Last Name :"+rs1.getString(3) );
     System.out.print( rs1.getString(4) );
          
     System.out.print(" => "+rs2.getString(2));
     System.out.print(" - "+rs2.getString(3));
    
     System.out.println(" ");
    
               ps.close();
                    } 
        } 
        }catch(SQLException e){
            
            JOptionPane.showMessageDialog(null, e);
       
        }
     
       
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: System.out.print date format

    I'm not a DB type but the ResultSet interface provides a getDate command that returns a java.sql.Date object. Perhaps you can use that and format it using the SimpleDateFormat class. Since java.sql.Date subclasses java.util.Date, it seems like it should work.

    Regards,
    Jim
    Last edited by jim829; 02-25-2017 at 02:07 AM. Reason: typo
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: System.out.print date format

    the result of this is 2017-01-07 13:08:35.000000 .
    Is the result you show in a String? The SimpleDateFormat class's parse() method could create a Date object that could be formatted as per Jim's post.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: System.out.print date format

    A few points.
    Do not use the index version of getXXX on a ResultSet when you have a 'SELECT * FROM...' query, as you cannot guarantee what order the columns are returned.
    Either use the version where you pass in the column name, or give the column names in the SELECT statement.

    When getting the data from the columns try and use the actual datatype. Since this is a DATE (or TIMESTAMP) column, then you should be using the getDate method (as jim suggests), otherwise it will reformat the DATE using whatever default format the JDBC driver uses for that database.

    Finally, you are not closing your resources (PreparedStatement and ResultSet and, possibly, the Connection?) in a finally block. You can use try-with-resources instead, though, which skips some of the boiler plate code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 01-07-2013, 02:48 PM
  2. system.out.printf versus system.out.format
    By bigsonny in forum New To Java
    Replies: 10
    Last Post: 06-21-2011, 10:40 PM
  3. Format date
    By mine0926 in forum New To Java
    Replies: 11
    Last Post: 08-04-2010, 01:56 AM
  4. julian date to full date format
    By judy318 in forum New To Java
    Replies: 7
    Last Post: 11-02-2009, 12:17 PM
  5. Default/System Date Format Pattern?
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-05-2008, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •