6 Replies Latest reply: Nov 29, 2010 8:16 AM by tschodt RSS

    how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY

    819402

      i am using informix database which accepts date value in the form of DATE format......
      the other part of my application takes date from the field in DD/MM/YYYY format...so i have to convert my java.sql.date in YYYY/MM/DD fromat into DD/MM/YYYY fromat of same type before inserting into db......
      but using parse method in SimpleDateFormat class can get the result only in java.util.date format...
      and also using format method can result only in string conversion........

        • 1. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
          tschodt
          816399 wrote:
          i am using informix database which accepts date value in the form of DATE format......
          Huh?
          Maybe you mean Informix (fronted by JDBC) expects date values as java.sql.Date objects?
          the other part of my application takes date from the field in DD/MM/YYYY format...
          so i have to convert my java.sql.date in YYYY/MM/DD format into DD/MM/YYYY format of same type before inserting into db......
          I am not sure why you are talking about formats here.
          There is no formatting inherent in a java.util.Date object
          nor in a java.sql.Date object.
          but using parse method in SimpleDateFormat class can get the result only in java.util.date format...
          and also using format method can result only in string conversion........
          You can easily create a java.sql.Date object from a java.util.Date object.
          String s = "31/12/2010";
          java.util.Date ud = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(s);
          java.sql.Date sd = new java.sql.Date(ud.getTime());
          //
          ud = sd; // java.sql.Date extends java.util.Date so no conversion is needed
          • 2. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
            PhHein
            Look at the constructors of java.sql.Date. You can instanciate java.sqlDtae like this:
            Date sqlDate = new Date(new java.util.Date().getTime()); 
            EDIT: what tschodt said.
            • 3. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
              819402
              thnx for the reply guyz.........I will work on it and reply the result as soon as possible.......
              • 4. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
                819402
                Consider this as my source file:


                import java.text.SimpleDateFormat;
                public class Dbop {
                     public static void main(String[] args) {
                          String s = "31/12/2010";
                          try{
                               java.util.Date ud = new java.text.SimpleDateFormat("dd/MM/yyyy").parse(s);
                               java.sql.Date sd = new java.sql.Date(ud.getTime());
                               //
                               ud = sd;     
                               System.out.println("result:"+ud);
                          }
                          catch(Exception e){
                               
                          }
                     }
                }

                i have added this code to my simple program and executed.......the result is like

                result:2010-12-31

                but i want in dd/mm/yyyy format as java.sql.date type........

                Edited by: 816399 on Nov 29, 2010 4:36 AM
                • 5. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
                  PhHein
                  If you want it in a specific format you need to format your date to a String. A sysout(ud) will just give you some formatted String. The driver/PreparedStatement will take care of formatting the date.
                  • 6. Re: how to convert JAVA.SQL.DATE date in YYYY/MM/DD format into DD/MM/YYYY
                    tschodt
                    public class Dbop {
                        static final java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("dd/MM/yyyy");
                        public static void main(String[] args) throws Exception {
                            java.util.Date ud = fmt.parse("31/12/2010");
                            java.sql.Date sd = new java.sql.Date(ud.getTime());
                            String formattedSqlDate = fmt.format(sd);
                            System.out.println("result:"+formattedSqlDate);
                        }
                    }