Convert String To Java.sql.Date












2















After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.



So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.



When the program is running he can select a certain date to query the database and so here the problem



Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string



Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect



Thank You,










share|improve this question



























    2















    After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.



    So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.



    When the program is running he can select a certain date to query the database and so here the problem



    Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string



    Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect



    Thank You,










    share|improve this question

























      2












      2








      2








      After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.



      So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.



      When the program is running he can select a certain date to query the database and so here the problem



      Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string



      Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect



      Thank You,










      share|improve this question














      After looking through many of the threads in stackoverflow and cant seem to find the correct method im looking for. If anyone could shine a bit of light it would be great.



      So I populated a JCombo Box with SQL Dates but in doing so I formatted them to using SimpleDateFormat which converted them into a string with the format dd/MM/yyyy.



      When the program is running he can select a certain date to query the database and so here the problem



      Ive managed to make a String with a format of yyyy/MM/dd using string.split saving it into an array and ordering them in another string



      Is There A Way To Convert This String (2014/04/13) into an SQL Date so I can query my database OR am I going about it incorrect



      Thank You,







      java mysql string date jdbc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 13 '14 at 19:12









      OMUIRIOMUIRI

      1613




      1613
























          3 Answers
          3






          active

          oldest

          votes


















          3















          Is There A Way To Convert This String (2014/04/13) into an SQL Date?




          Sample code:



              SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

          try {
          java.util.Date utilDate = format.parse("2014/04/13");
          java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
          System.out.println(sqlDate);
          } catch (ParseException e) {
          e.printStackTrace();
          }


          output:



          2014-04-13



          use SimpleDateFormat to parse a string into java.util.Date.



          use java.sql.Date(miliseconds) constructor.







          share|improve this answer


























          • Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

            – OMUIRI
            Apr 13 '14 at 19:30











          • yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

            – Braj
            Apr 13 '14 at 19:32











          • I logic behind is the milliseconds that is used in constructor of both the classes.

            – Braj
            Apr 13 '14 at 19:33











          • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

            – Braj
            Apr 13 '14 at 19:34











          • Is there any date in your database that is older than January 1, 1970?

            – Braj
            Apr 13 '14 at 19:34



















          2














          Use java.util.SimpleDateFormat



          An example here:



              SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
          java.lang.Date langDate = sdf.parse("2014/04/13");
          java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());





          share|improve this answer


























          • Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

            – Braj
            Apr 13 '14 at 19:19













          • FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

            – Basil Bourque
            Nov 15 '18 at 5:44



















          0














          tl;dr



          myPreparedStatement.setObject( 
          … ,
          LocalDate.parse(
          "23/01/2018" ,
          DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
          )
          ) ;


          java.time



          The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.



          The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.



          Define a formatting pattern to match your input strings.



          String input = "23/01/2018" ; 
          DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
          LocalDate localDate = LocalDate.parse( input , f ) ;
          String output = localDate.toString() ; // Generate text in standard ISO 8601 format.


          JDBC 4.2



          As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.



          myPreparedStatement.setObject( … , localDate ) ;




          About java.time



          The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



          The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



          To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



          You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



          Where to obtain the java.time classes?





          • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


            • Java 9 adds some minor features and fixes.




          • Java SE 6 and Java SE 7


            • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




          • Android


            • Later versions of Android bundle implementations of the java.time classes.

            • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




          The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23047200%2fconvert-string-to-java-sql-date%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3















            Is There A Way To Convert This String (2014/04/13) into an SQL Date?




            Sample code:



                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            try {
            java.util.Date utilDate = format.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            System.out.println(sqlDate);
            } catch (ParseException e) {
            e.printStackTrace();
            }


            output:



            2014-04-13



            use SimpleDateFormat to parse a string into java.util.Date.



            use java.sql.Date(miliseconds) constructor.







            share|improve this answer


























            • Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

              – OMUIRI
              Apr 13 '14 at 19:30











            • yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

              – Braj
              Apr 13 '14 at 19:32











            • I logic behind is the milliseconds that is used in constructor of both the classes.

              – Braj
              Apr 13 '14 at 19:33











            • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

              – Braj
              Apr 13 '14 at 19:34











            • Is there any date in your database that is older than January 1, 1970?

              – Braj
              Apr 13 '14 at 19:34
















            3















            Is There A Way To Convert This String (2014/04/13) into an SQL Date?




            Sample code:



                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            try {
            java.util.Date utilDate = format.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            System.out.println(sqlDate);
            } catch (ParseException e) {
            e.printStackTrace();
            }


            output:



            2014-04-13



            use SimpleDateFormat to parse a string into java.util.Date.



            use java.sql.Date(miliseconds) constructor.







            share|improve this answer


























            • Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

              – OMUIRI
              Apr 13 '14 at 19:30











            • yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

              – Braj
              Apr 13 '14 at 19:32











            • I logic behind is the milliseconds that is used in constructor of both the classes.

              – Braj
              Apr 13 '14 at 19:33











            • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

              – Braj
              Apr 13 '14 at 19:34











            • Is there any date in your database that is older than January 1, 1970?

              – Braj
              Apr 13 '14 at 19:34














            3












            3








            3








            Is There A Way To Convert This String (2014/04/13) into an SQL Date?




            Sample code:



                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            try {
            java.util.Date utilDate = format.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            System.out.println(sqlDate);
            } catch (ParseException e) {
            e.printStackTrace();
            }


            output:



            2014-04-13



            use SimpleDateFormat to parse a string into java.util.Date.



            use java.sql.Date(miliseconds) constructor.







            share|improve this answer
















            Is There A Way To Convert This String (2014/04/13) into an SQL Date?




            Sample code:



                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

            try {
            java.util.Date utilDate = format.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            System.out.println(sqlDate);
            } catch (ParseException e) {
            e.printStackTrace();
            }


            output:



            2014-04-13



            use SimpleDateFormat to parse a string into java.util.Date.



            use java.sql.Date(miliseconds) constructor.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '14 at 19:27

























            answered Apr 13 '14 at 19:15









            BrajBraj

            40.7k53959




            40.7k53959













            • Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

              – OMUIRI
              Apr 13 '14 at 19:30











            • yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

              – Braj
              Apr 13 '14 at 19:32











            • I logic behind is the milliseconds that is used in constructor of both the classes.

              – Braj
              Apr 13 '14 at 19:33











            • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

              – Braj
              Apr 13 '14 at 19:34











            • Is there any date in your database that is older than January 1, 1970?

              – Braj
              Apr 13 '14 at 19:34



















            • Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

              – OMUIRI
              Apr 13 '14 at 19:30











            • yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

              – Braj
              Apr 13 '14 at 19:32











            • I logic behind is the milliseconds that is used in constructor of both the classes.

              – Braj
              Apr 13 '14 at 19:33











            • getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

              – Braj
              Apr 13 '14 at 19:34











            • Is there any date in your database that is older than January 1, 1970?

              – Braj
              Apr 13 '14 at 19:34

















            Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

            – OMUIRI
            Apr 13 '14 at 19:30





            Thanks Braj works a treat I just slotted my formatted String in instead of "2014/04/13". It was really bugging me for the last 2 hours. Did not think of bring in java.util.date even though java.sql.date is a subclass. Cheers again

            – OMUIRI
            Apr 13 '14 at 19:30













            yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

            – Braj
            Apr 13 '14 at 19:32





            yes you can do it in same manner java.util.Date newUtilDate=new java.util.Date(sqlDate.getTime());

            – Braj
            Apr 13 '14 at 19:32













            I logic behind is the milliseconds that is used in constructor of both the classes.

            – Braj
            Apr 13 '14 at 19:33





            I logic behind is the milliseconds that is used in constructor of both the classes.

            – Braj
            Apr 13 '14 at 19:33













            getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

            – Braj
            Apr 13 '14 at 19:34





            getTime() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

            – Braj
            Apr 13 '14 at 19:34













            Is there any date in your database that is older than January 1, 1970?

            – Braj
            Apr 13 '14 at 19:34





            Is there any date in your database that is older than January 1, 1970?

            – Braj
            Apr 13 '14 at 19:34













            2














            Use java.util.SimpleDateFormat



            An example here:



                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            java.lang.Date langDate = sdf.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());





            share|improve this answer


























            • Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

              – Braj
              Apr 13 '14 at 19:19













            • FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

              – Basil Bourque
              Nov 15 '18 at 5:44
















            2














            Use java.util.SimpleDateFormat



            An example here:



                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            java.lang.Date langDate = sdf.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());





            share|improve this answer


























            • Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

              – Braj
              Apr 13 '14 at 19:19













            • FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

              – Basil Bourque
              Nov 15 '18 at 5:44














            2












            2








            2







            Use java.util.SimpleDateFormat



            An example here:



                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            java.lang.Date langDate = sdf.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());





            share|improve this answer















            Use java.util.SimpleDateFormat



            An example here:



                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            java.lang.Date langDate = sdf.parse("2014/04/13");
            java.sql.Date sqlDate = new java.sql.Date(langDate.getTime());






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 24 '18 at 15:34









            Kurohige

            5981520




            5981520










            answered Apr 13 '14 at 19:15









            AndresAndres

            8,57732547




            8,57732547













            • Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

              – Braj
              Apr 13 '14 at 19:19













            • FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

              – Basil Bourque
              Nov 15 '18 at 5:44



















            • Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

              – Braj
              Apr 13 '14 at 19:19













            • FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

              – Basil Bourque
              Nov 15 '18 at 5:44

















            Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

            – Braj
            Apr 13 '14 at 19:19







            Sorry you have used mm in place of MM. That is wrong. I have already posted it. Please look into again.

            – Braj
            Apr 13 '14 at 19:19















            FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

            – Basil Bourque
            Nov 15 '18 at 5:44





            FYI, the terribly-designed java.sql.Date class has been supplanted by java.time.LocalDate.

            – Basil Bourque
            Nov 15 '18 at 5:44











            0














            tl;dr



            myPreparedStatement.setObject( 
            … ,
            LocalDate.parse(
            "23/01/2018" ,
            DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
            )
            ) ;


            java.time



            The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.



            The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.



            Define a formatting pattern to match your input strings.



            String input = "23/01/2018" ; 
            DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
            LocalDate localDate = LocalDate.parse( input , f ) ;
            String output = localDate.toString() ; // Generate text in standard ISO 8601 format.


            JDBC 4.2



            As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.



            myPreparedStatement.setObject( … , localDate ) ;




            About java.time



            The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



            The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



            To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



            You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



            Where to obtain the java.time classes?





            • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


              • Java 9 adds some minor features and fixes.




            • Java SE 6 and Java SE 7


              • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




            • Android


              • Later versions of Android bundle implementations of the java.time classes.

              • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




            The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






            share|improve this answer




























              0














              tl;dr



              myPreparedStatement.setObject( 
              … ,
              LocalDate.parse(
              "23/01/2018" ,
              DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
              )
              ) ;


              java.time



              The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.



              The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.



              Define a formatting pattern to match your input strings.



              String input = "23/01/2018" ; 
              DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
              LocalDate localDate = LocalDate.parse( input , f ) ;
              String output = localDate.toString() ; // Generate text in standard ISO 8601 format.


              JDBC 4.2



              As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.



              myPreparedStatement.setObject( … , localDate ) ;




              About java.time



              The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



              The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



              To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



              You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



              Where to obtain the java.time classes?





              • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


                • Java 9 adds some minor features and fixes.




              • Java SE 6 and Java SE 7


                • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




              • Android


                • Later versions of Android bundle implementations of the java.time classes.

                • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




              The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






              share|improve this answer


























                0












                0








                0







                tl;dr



                myPreparedStatement.setObject( 
                … ,
                LocalDate.parse(
                "23/01/2018" ,
                DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
                )
                ) ;


                java.time



                The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.



                The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.



                Define a formatting pattern to match your input strings.



                String input = "23/01/2018" ; 
                DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
                LocalDate localDate = LocalDate.parse( input , f ) ;
                String output = localDate.toString() ; // Generate text in standard ISO 8601 format.


                JDBC 4.2



                As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.



                myPreparedStatement.setObject( … , localDate ) ;




                About java.time



                The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



                The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



                To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



                You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



                Where to obtain the java.time classes?





                • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


                  • Java 9 adds some minor features and fixes.




                • Java SE 6 and Java SE 7


                  • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




                • Android


                  • Later versions of Android bundle implementations of the java.time classes.

                  • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




                The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






                share|improve this answer













                tl;dr



                myPreparedStatement.setObject( 
                … ,
                LocalDate.parse(
                "23/01/2018" ,
                DateTimeFormatter.ofPattern( "dd/MM/uuuu" )
                )
                ) ;


                java.time



                The modern approach uses the java.time classes that supplanted the terrible Date/Calendar/SimpleDateFormat classes.



                The LocalDate class represents a date-only value without time-of-day and without time zone or offset-from-UTC.



                Define a formatting pattern to match your input strings.



                String input = "23/01/2018" ; 
                DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
                LocalDate localDate = LocalDate.parse( input , f ) ;
                String output = localDate.toString() ; // Generate text in standard ISO 8601 format.


                JDBC 4.2



                As of JDBC 4.2, we can exchange java.time objects with the database. Use smart objects rather than dumb text for database interactions.



                myPreparedStatement.setObject( … , localDate ) ;




                About java.time



                The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



                The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



                To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



                You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



                Where to obtain the java.time classes?





                • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


                  • Java 9 adds some minor features and fixes.




                • Java SE 6 and Java SE 7


                  • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




                • Android


                  • Later versions of Android bundle implementations of the java.time classes.

                  • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




                The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 5:42









                Basil BourqueBasil Bourque

                113k29388547




                113k29388547






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23047200%2fconvert-string-to-java-sql-date%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    The Sandy Post

                    Danny Elfman

                    Pages that link to "Head v. Amoskeag Manufacturing Co."