java to mysql. I need convert from string parametre to timestamp










2















I'm trying to parser String to Timestamp because I need to save this data on bbdd mysql.



String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());

catch (ParseException e)
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());



When I run my app I get this catch message:




ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"




Can anybody help me?










share|improve this question
























  • First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

    – Ole V.V.
    Nov 14 '18 at 14:09
















2















I'm trying to parser String to Timestamp because I need to save this data on bbdd mysql.



String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());

catch (ParseException e)
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());



When I run my app I get this catch message:




ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"




Can anybody help me?










share|improve this question
























  • First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

    – Ole V.V.
    Nov 14 '18 at 14:09














2












2








2








I'm trying to parser String to Timestamp because I need to save this data on bbdd mysql.



String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());

catch (ParseException e)
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());



When I run my app I get this catch message:




ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"




Can anybody help me?










share|improve this question
















I'm trying to parser String to Timestamp because I need to save this data on bbdd mysql.



String dateString: "2018-10-17T22:37:10.000+0000";
java.sql.Timestamp timeStampDate = null;
try
DateFormat formatter;
formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date date = (Date) formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());

catch (ParseException e)
log.debug("ERROR parser String to Timestamp to save bbdd. ", e.getMessage());



When I run my app I get this catch message:




ERROR parser String to Timestamp to save bbdd. Unparseable date: "2018-10-17T22:37:10.000+0000"




Can anybody help me?







java timestamp simpledateformat datetime-parsing unparseable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:32









Ole V.V.

29.6k63653




29.6k63653










asked Nov 14 '18 at 0:01









KaraKara

275




275












  • First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

    – Ole V.V.
    Nov 14 '18 at 14:09


















  • First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

    – Ole V.V.
    Nov 14 '18 at 14:09

















First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

– Ole V.V.
Nov 14 '18 at 14:09






First, I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Second, for saving into MySQL it’s probably better to save an Instant, an OffsetDateTime or a LocalDateTimethan a Timestamp (another poorly designed and outdated class).

– Ole V.V.
Nov 14 '18 at 14:09













2 Answers
2






active

oldest

votes


















2














change your mask to



DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");



so you have



java.sql.Timestamp timeStampDate = null;
String dateString = "2018-10-17T22:37:10.000+0000";

try
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = formatter.parse(dateString);
timeStampDate = new Timestamp(date.getTime());

catch (ParseException e)
e.printStackTrace();



By the way you should not need to cast the Date



Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ






share|improve this answer

























  • thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

    – Kara
    Nov 14 '18 at 0:36







  • 1





    Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

    – Andreas
    Nov 14 '18 at 0:48


















0














java.time



 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
String dateString = "2018-10-17T22:37:10.000+0000";
OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);

System.out.println("Parsed datetime: " + odt);


Output from this code is:




Parsed datetime: 2018-10-17T22:37:10Z




For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:



 PreparedStatement ps = myDatabaseConnection.prepareStatement(
"insert into my_table (my_timestamp) values (?)");
ps.setObject(1, odt);


Link: Oracle tutorial: Date Time explaining how to use java.time.






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%2f53291240%2fjava-to-mysql-i-need-convert-from-string-parametre-to-timestamp%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














    change your mask to



    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");



    so you have



    java.sql.Timestamp timeStampDate = null;
    String dateString = "2018-10-17T22:37:10.000+0000";

    try
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date date = formatter.parse(dateString);
    timeStampDate = new Timestamp(date.getTime());

    catch (ParseException e)
    e.printStackTrace();



    By the way you should not need to cast the Date



    Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ






    share|improve this answer

























    • thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

      – Kara
      Nov 14 '18 at 0:36







    • 1





      Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

      – Andreas
      Nov 14 '18 at 0:48















    2














    change your mask to



    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");



    so you have



    java.sql.Timestamp timeStampDate = null;
    String dateString = "2018-10-17T22:37:10.000+0000";

    try
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date date = formatter.parse(dateString);
    timeStampDate = new Timestamp(date.getTime());

    catch (ParseException e)
    e.printStackTrace();



    By the way you should not need to cast the Date



    Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ






    share|improve this answer

























    • thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

      – Kara
      Nov 14 '18 at 0:36







    • 1





      Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

      – Andreas
      Nov 14 '18 at 0:48













    2












    2








    2







    change your mask to



    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");



    so you have



    java.sql.Timestamp timeStampDate = null;
    String dateString = "2018-10-17T22:37:10.000+0000";

    try
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date date = formatter.parse(dateString);
    timeStampDate = new Timestamp(date.getTime());

    catch (ParseException e)
    e.printStackTrace();



    By the way you should not need to cast the Date



    Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ






    share|improve this answer















    change your mask to



    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");



    so you have



    java.sql.Timestamp timeStampDate = null;
    String dateString = "2018-10-17T22:37:10.000+0000";

    try
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date date = formatter.parse(dateString);
    timeStampDate = new Timestamp(date.getTime());

    catch (ParseException e)
    e.printStackTrace();



    By the way you should not need to cast the Date



    Apologies for my slackness, in my haste I did not test the output and as per @andreas comment, the correct mask is actually yyyy-MM-dd'T'HH:mm:ss.SSSZ







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 23:49

























    answered Nov 14 '18 at 0:10









    Scary WombatScary Wombat

    35.3k32252




    35.3k32252












    • thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

      – Kara
      Nov 14 '18 at 0:36







    • 1





      Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

      – Andreas
      Nov 14 '18 at 0:48

















    • thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

      – Kara
      Nov 14 '18 at 0:36







    • 1





      Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

      – Andreas
      Nov 14 '18 at 0:48
















    thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

    – Kara
    Nov 14 '18 at 0:36






    thanks @Scary. Now it is working. and yes, I dont need to cast the Date.

    – Kara
    Nov 14 '18 at 0:36





    1




    1





    Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

    – Andreas
    Nov 14 '18 at 0:48





    Sorry, that is wrong. First, it's HH, not hh. Second, ignoring the +0 time zone offset means you get the wrong time, e.g. I'm on US east coast, so running that code would get me time 2018-10-17 22:37:10.000 EDT, which is not the same as time 2018-10-17T22:37:10.000+0000, because that would be 2018-10-17 18:37:10.000 EDT. Correct format string is yyyy-MM-dd'T'HH:mm:ss.SSSZ

    – Andreas
    Nov 14 '18 at 0:48













    0














    java.time



     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
    String dateString = "2018-10-17T22:37:10.000+0000";
    OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);

    System.out.println("Parsed datetime: " + odt);


    Output from this code is:




    Parsed datetime: 2018-10-17T22:37:10Z




    For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:



     PreparedStatement ps = myDatabaseConnection.prepareStatement(
    "insert into my_table (my_timestamp) values (?)");
    ps.setObject(1, odt);


    Link: Oracle tutorial: Date Time explaining how to use java.time.






    share|improve this answer



























      0














      java.time



       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
      String dateString = "2018-10-17T22:37:10.000+0000";
      OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);

      System.out.println("Parsed datetime: " + odt);


      Output from this code is:




      Parsed datetime: 2018-10-17T22:37:10Z




      For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:



       PreparedStatement ps = myDatabaseConnection.prepareStatement(
      "insert into my_table (my_timestamp) values (?)");
      ps.setObject(1, odt);


      Link: Oracle tutorial: Date Time explaining how to use java.time.






      share|improve this answer

























        0












        0








        0







        java.time



         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
        String dateString = "2018-10-17T22:37:10.000+0000";
        OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);

        System.out.println("Parsed datetime: " + odt);


        Output from this code is:




        Parsed datetime: 2018-10-17T22:37:10Z




        For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:



         PreparedStatement ps = myDatabaseConnection.prepareStatement(
        "insert into my_table (my_timestamp) values (?)");
        ps.setObject(1, odt);


        Link: Oracle tutorial: Date Time explaining how to use java.time.






        share|improve this answer













        java.time



         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXX");
        String dateString = "2018-10-17T22:37:10.000+0000";
        OffsetDateTime odt = OffsetDateTime.parse(dateString, formatter);

        System.out.println("Parsed datetime: " + odt);


        Output from this code is:




        Parsed datetime: 2018-10-17T22:37:10Z




        For saving into MySQL it’s good to use a datetime object, but the Timestamp class has design problems and is now long outdated. I am sorry that I don’t have the experience with MySQL, but I think the following should work:



         PreparedStatement ps = myDatabaseConnection.prepareStatement(
        "insert into my_table (my_timestamp) values (?)");
        ps.setObject(1, odt);


        Link: Oracle tutorial: Date Time explaining how to use java.time.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 14:30









        Ole V.V.Ole V.V.

        29.6k63653




        29.6k63653



























            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%2f53291240%2fjava-to-mysql-i-need-convert-from-string-parametre-to-timestamp%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo