Inserting into database a datetime with the current UTC time












0















I'm attempting to insert a datetime into a mysql database for the current UTC time via the GETUTCDATE() function in sql. It's failing with "FUNCTION GETUTCDATE DOES NOT EXIST".



Is a way for me to get the current UTC time in sql datetime format from Java, and simply insert it as a string?



Another big issue I'm having is I need to convert the above utc datetime object into local time zones and I don't really know how to do that through standard java api's.










share|improve this question























  • have you tried providing a value from Java?

    – Scary Wombat
    Nov 16 '18 at 2:21











  • I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

    – Joe
    Nov 16 '18 at 2:46






  • 1





    Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

    – Ole V.V.
    Nov 16 '18 at 13:14











  • If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

    – Ole V.V.
    Nov 16 '18 at 21:47
















0















I'm attempting to insert a datetime into a mysql database for the current UTC time via the GETUTCDATE() function in sql. It's failing with "FUNCTION GETUTCDATE DOES NOT EXIST".



Is a way for me to get the current UTC time in sql datetime format from Java, and simply insert it as a string?



Another big issue I'm having is I need to convert the above utc datetime object into local time zones and I don't really know how to do that through standard java api's.










share|improve this question























  • have you tried providing a value from Java?

    – Scary Wombat
    Nov 16 '18 at 2:21











  • I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

    – Joe
    Nov 16 '18 at 2:46






  • 1





    Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

    – Ole V.V.
    Nov 16 '18 at 13:14











  • If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

    – Ole V.V.
    Nov 16 '18 at 21:47














0












0








0








I'm attempting to insert a datetime into a mysql database for the current UTC time via the GETUTCDATE() function in sql. It's failing with "FUNCTION GETUTCDATE DOES NOT EXIST".



Is a way for me to get the current UTC time in sql datetime format from Java, and simply insert it as a string?



Another big issue I'm having is I need to convert the above utc datetime object into local time zones and I don't really know how to do that through standard java api's.










share|improve this question














I'm attempting to insert a datetime into a mysql database for the current UTC time via the GETUTCDATE() function in sql. It's failing with "FUNCTION GETUTCDATE DOES NOT EXIST".



Is a way for me to get the current UTC time in sql datetime format from Java, and simply insert it as a string?



Another big issue I'm having is I need to convert the above utc datetime object into local time zones and I don't really know how to do that through standard java api's.







java mysql datetime java-8






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 2:10









JoeJoe

1278




1278













  • have you tried providing a value from Java?

    – Scary Wombat
    Nov 16 '18 at 2:21











  • I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

    – Joe
    Nov 16 '18 at 2:46






  • 1





    Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

    – Ole V.V.
    Nov 16 '18 at 13:14











  • If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

    – Ole V.V.
    Nov 16 '18 at 21:47



















  • have you tried providing a value from Java?

    – Scary Wombat
    Nov 16 '18 at 2:21











  • I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

    – Joe
    Nov 16 '18 at 2:46






  • 1





    Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

    – Ole V.V.
    Nov 16 '18 at 13:14











  • If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

    – Ole V.V.
    Nov 16 '18 at 21:47

















have you tried providing a value from Java?

– Scary Wombat
Nov 16 '18 at 2:21





have you tried providing a value from Java?

– Scary Wombat
Nov 16 '18 at 2:21













I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

– Joe
Nov 16 '18 at 2:46





I don't know what a datetime from sql corresponds to in Java, so I'm not sure what to pass.

– Joe
Nov 16 '18 at 2:46




1




1





Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

– Ole V.V.
Nov 16 '18 at 13:14





Assuming that in MySQL you are using datetime or another appropriate date-time datatype (as you should), don’t insert a string. If a value from Java, give it as a proper date-time type from java.time, for example OffsetDateTime as in the answer by Basil Bourque.

– Ole V.V.
Nov 16 '18 at 13:14













If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

– Ole V.V.
Nov 16 '18 at 21:47





If your type in MySQL is datetime, I believe LocalDateTime in Java will be the best fit, but beware that neither defines a unique point in time since they don’t include time zone.

– Ole V.V.
Nov 16 '18 at 21:47












2 Answers
2






active

oldest

votes


















2














tl;dr



myPreparedStatement      // Using a `PreparedStatement` avoids SQL-injection security risk.
.setObject( // As of JDBC 4.2, we can exchange java.time objects with a database via `getObject`/`setObject` methods.
… , // Indicate which `?` placeholder in your SQL statement.
OffsetDateTime.now( ZoneOffset.UTC ) // Capture the current moment in UTC.
) ;


java.time



The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes.



Get the current moment in UTC using OffsetDateTime.



OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;


MySQL 8.0 uses a resolution of microseconds, for six decimal places in a fractional second. The java.time classes carry a finer resolution of nanoseconds. So you may want to truncate any existing nanos from your OffsetDateTime. Specify your desired resolution with ChronoUnit.



OffsetDateTime odt = 
OffsetDateTime
.now(
ZoneOffset.UTC
)
.truncatedTo( ChronoUnit.MICROS )
;


Send to your database via a PreparedStatement to a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE data type. For MySQL 8.0, that would be the type TIMESTAMP.



myPreparedStatement.setObject( … , odt ) ;


And retrieval via a ResultSet.



OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


To see this moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId to get a ZonedDateTime object.



ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; // Same moment, same point on the timeline, different wall-clock time.




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














    You can do it as follows:



    OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
    String sql_date = utc.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); //here, you can change the format of SQL date as you need


    You would need to import the classes as follows:



    import java.time.OffsetDateTime;
    import java.time.ZoneOffset;
    import java.time.format.DateTimeFormatter;


    Hope it helps.






    share|improve this answer


























    • why do you format it as a String?

      – Scary Wombat
      Nov 16 '18 at 3:32











    • The question asked for that: "and simply insert it as a string"

      – TeeKea
      Nov 16 '18 at 3:34













    • Sorry missed that part didn't I?

      – Scary Wombat
      Nov 16 '18 at 3:56











    • The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

      – Scary Wombat
      Nov 16 '18 at 3:59











    • No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

      – Basil Bourque
      Nov 16 '18 at 4:02













    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%2f53330472%2finserting-into-database-a-datetime-with-the-current-utc-time%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    tl;dr



    myPreparedStatement      // Using a `PreparedStatement` avoids SQL-injection security risk.
    .setObject( // As of JDBC 4.2, we can exchange java.time objects with a database via `getObject`/`setObject` methods.
    … , // Indicate which `?` placeholder in your SQL statement.
    OffsetDateTime.now( ZoneOffset.UTC ) // Capture the current moment in UTC.
    ) ;


    java.time



    The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes.



    Get the current moment in UTC using OffsetDateTime.



    OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;


    MySQL 8.0 uses a resolution of microseconds, for six decimal places in a fractional second. The java.time classes carry a finer resolution of nanoseconds. So you may want to truncate any existing nanos from your OffsetDateTime. Specify your desired resolution with ChronoUnit.



    OffsetDateTime odt = 
    OffsetDateTime
    .now(
    ZoneOffset.UTC
    )
    .truncatedTo( ChronoUnit.MICROS )
    ;


    Send to your database via a PreparedStatement to a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE data type. For MySQL 8.0, that would be the type TIMESTAMP.



    myPreparedStatement.setObject( … , odt ) ;


    And retrieval via a ResultSet.



    OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


    To see this moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId to get a ZonedDateTime object.



    ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
    ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; // Same moment, same point on the timeline, different wall-clock time.




    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






























      2














      tl;dr



      myPreparedStatement      // Using a `PreparedStatement` avoids SQL-injection security risk.
      .setObject( // As of JDBC 4.2, we can exchange java.time objects with a database via `getObject`/`setObject` methods.
      … , // Indicate which `?` placeholder in your SQL statement.
      OffsetDateTime.now( ZoneOffset.UTC ) // Capture the current moment in UTC.
      ) ;


      java.time



      The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes.



      Get the current moment in UTC using OffsetDateTime.



      OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;


      MySQL 8.0 uses a resolution of microseconds, for six decimal places in a fractional second. The java.time classes carry a finer resolution of nanoseconds. So you may want to truncate any existing nanos from your OffsetDateTime. Specify your desired resolution with ChronoUnit.



      OffsetDateTime odt = 
      OffsetDateTime
      .now(
      ZoneOffset.UTC
      )
      .truncatedTo( ChronoUnit.MICROS )
      ;


      Send to your database via a PreparedStatement to a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE data type. For MySQL 8.0, that would be the type TIMESTAMP.



      myPreparedStatement.setObject( … , odt ) ;


      And retrieval via a ResultSet.



      OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


      To see this moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId to get a ZonedDateTime object.



      ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
      ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; // Same moment, same point on the timeline, different wall-clock time.




      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




























        2












        2








        2







        tl;dr



        myPreparedStatement      // Using a `PreparedStatement` avoids SQL-injection security risk.
        .setObject( // As of JDBC 4.2, we can exchange java.time objects with a database via `getObject`/`setObject` methods.
        … , // Indicate which `?` placeholder in your SQL statement.
        OffsetDateTime.now( ZoneOffset.UTC ) // Capture the current moment in UTC.
        ) ;


        java.time



        The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes.



        Get the current moment in UTC using OffsetDateTime.



        OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;


        MySQL 8.0 uses a resolution of microseconds, for six decimal places in a fractional second. The java.time classes carry a finer resolution of nanoseconds. So you may want to truncate any existing nanos from your OffsetDateTime. Specify your desired resolution with ChronoUnit.



        OffsetDateTime odt = 
        OffsetDateTime
        .now(
        ZoneOffset.UTC
        )
        .truncatedTo( ChronoUnit.MICROS )
        ;


        Send to your database via a PreparedStatement to a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE data type. For MySQL 8.0, that would be the type TIMESTAMP.



        myPreparedStatement.setObject( … , odt ) ;


        And retrieval via a ResultSet.



        OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


        To see this moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId to get a ZonedDateTime object.



        ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
        ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; // Same moment, same point on the timeline, different wall-clock time.




        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      // Using a `PreparedStatement` avoids SQL-injection security risk.
        .setObject( // As of JDBC 4.2, we can exchange java.time objects with a database via `getObject`/`setObject` methods.
        … , // Indicate which `?` placeholder in your SQL statement.
        OffsetDateTime.now( ZoneOffset.UTC ) // Capture the current moment in UTC.
        ) ;


        java.time



        The modern solution uses the java.time classes that years ago supplanted the terrible old date-time classes.



        Get the current moment in UTC using OffsetDateTime.



        OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ;


        MySQL 8.0 uses a resolution of microseconds, for six decimal places in a fractional second. The java.time classes carry a finer resolution of nanoseconds. So you may want to truncate any existing nanos from your OffsetDateTime. Specify your desired resolution with ChronoUnit.



        OffsetDateTime odt = 
        OffsetDateTime
        .now(
        ZoneOffset.UTC
        )
        .truncatedTo( ChronoUnit.MICROS )
        ;


        Send to your database via a PreparedStatement to a column of a type akin to the SQL-standard TIMESTAMP WITH TIME ZONE data type. For MySQL 8.0, that would be the type TIMESTAMP.



        myPreparedStatement.setObject( … , odt ) ;


        And retrieval via a ResultSet.



        OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;


        To see this moment through the lens of the wall-clock time used by the people of a particular region (a time zone), apply a ZoneId to get a ZonedDateTime object.



        ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
        ZonedDateTime zdt = odt.atZoneSameInstant( z ) ; // Same moment, same point on the timeline, different wall-clock time.




        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








        edited Nov 16 '18 at 16:41

























        answered Nov 16 '18 at 4:06









        Basil BourqueBasil Bourque

        115k29394557




        115k29394557

























            0














            You can do it as follows:



            OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
            String sql_date = utc.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); //here, you can change the format of SQL date as you need


            You would need to import the classes as follows:



            import java.time.OffsetDateTime;
            import java.time.ZoneOffset;
            import java.time.format.DateTimeFormatter;


            Hope it helps.






            share|improve this answer


























            • why do you format it as a String?

              – Scary Wombat
              Nov 16 '18 at 3:32











            • The question asked for that: "and simply insert it as a string"

              – TeeKea
              Nov 16 '18 at 3:34













            • Sorry missed that part didn't I?

              – Scary Wombat
              Nov 16 '18 at 3:56











            • The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

              – Scary Wombat
              Nov 16 '18 at 3:59











            • No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

              – Basil Bourque
              Nov 16 '18 at 4:02


















            0














            You can do it as follows:



            OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
            String sql_date = utc.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); //here, you can change the format of SQL date as you need


            You would need to import the classes as follows:



            import java.time.OffsetDateTime;
            import java.time.ZoneOffset;
            import java.time.format.DateTimeFormatter;


            Hope it helps.






            share|improve this answer


























            • why do you format it as a String?

              – Scary Wombat
              Nov 16 '18 at 3:32











            • The question asked for that: "and simply insert it as a string"

              – TeeKea
              Nov 16 '18 at 3:34













            • Sorry missed that part didn't I?

              – Scary Wombat
              Nov 16 '18 at 3:56











            • The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

              – Scary Wombat
              Nov 16 '18 at 3:59











            • No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

              – Basil Bourque
              Nov 16 '18 at 4:02
















            0












            0








            0







            You can do it as follows:



            OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
            String sql_date = utc.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); //here, you can change the format of SQL date as you need


            You would need to import the classes as follows:



            import java.time.OffsetDateTime;
            import java.time.ZoneOffset;
            import java.time.format.DateTimeFormatter;


            Hope it helps.






            share|improve this answer















            You can do it as follows:



            OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
            String sql_date = utc.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); //here, you can change the format of SQL date as you need


            You would need to import the classes as follows:



            import java.time.OffsetDateTime;
            import java.time.ZoneOffset;
            import java.time.format.DateTimeFormatter;


            Hope it helps.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 4:26

























            answered Nov 16 '18 at 2:50









            TeeKeaTeeKea

            3,22851932




            3,22851932













            • why do you format it as a String?

              – Scary Wombat
              Nov 16 '18 at 3:32











            • The question asked for that: "and simply insert it as a string"

              – TeeKea
              Nov 16 '18 at 3:34













            • Sorry missed that part didn't I?

              – Scary Wombat
              Nov 16 '18 at 3:56











            • The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

              – Scary Wombat
              Nov 16 '18 at 3:59











            • No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

              – Basil Bourque
              Nov 16 '18 at 4:02





















            • why do you format it as a String?

              – Scary Wombat
              Nov 16 '18 at 3:32











            • The question asked for that: "and simply insert it as a string"

              – TeeKea
              Nov 16 '18 at 3:34













            • Sorry missed that part didn't I?

              – Scary Wombat
              Nov 16 '18 at 3:56











            • The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

              – Scary Wombat
              Nov 16 '18 at 3:59











            • No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

              – Basil Bourque
              Nov 16 '18 at 4:02



















            why do you format it as a String?

            – Scary Wombat
            Nov 16 '18 at 3:32





            why do you format it as a String?

            – Scary Wombat
            Nov 16 '18 at 3:32













            The question asked for that: "and simply insert it as a string"

            – TeeKea
            Nov 16 '18 at 3:34







            The question asked for that: "and simply insert it as a string"

            – TeeKea
            Nov 16 '18 at 3:34















            Sorry missed that part didn't I?

            – Scary Wombat
            Nov 16 '18 at 3:56





            Sorry missed that part didn't I?

            – Scary Wombat
            Nov 16 '18 at 3:56













            The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

            – Scary Wombat
            Nov 16 '18 at 3:59





            The question doesn't really make sense does it. GETUTCDATE() returns a date time object, so why is he wanting a String? Maybe can you show him the proper way to go

            – Scary Wombat
            Nov 16 '18 at 3:59













            No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

            – Basil Bourque
            Nov 16 '18 at 4:02







            No, no, no, incorrect. You are mixing the modern java.time classes (OffsetDateTime, ZoneOffset) with the terrible old legacy classes ( SimpleDateFormat, java.util.Date, java.sql.Date).

            – Basil Bourque
            Nov 16 '18 at 4:02




















            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%2f53330472%2finserting-into-database-a-datetime-with-the-current-utc-time%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python