JAVA GSON date converter : How to get midnight as 00:00 AM instead of 12:00 AM












-3















I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?










share|improve this question

























  • Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

    – Ole V.V.
    Nov 26 '18 at 12:43











  • Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

    – Jean
    Nov 26 '18 at 13:32
















-3















I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?










share|improve this question

























  • Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

    – Ole V.V.
    Nov 26 '18 at 12:43











  • Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

    – Jean
    Nov 26 '18 at 13:32














-3












-3








-3








I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?










share|improve this question
















I use GSON to get a Json with different data. Everything works fine but when the user type 00:30:00 AM as half past midnight, it is converted as 12:30:00 AM. I d like to have it as 00:30:00 AM.
Is there a way to do without changing anything else in date conversion ?







java json date gson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:11







Jean

















asked Nov 26 '18 at 9:05









JeanJean

14611




14611













  • Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

    – Ole V.V.
    Nov 26 '18 at 12:43











  • Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

    – Jean
    Nov 26 '18 at 13:32



















  • Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

    – Ole V.V.
    Nov 26 '18 at 12:43











  • Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

    – Jean
    Nov 26 '18 at 13:32

















Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

– Ole V.V.
Nov 26 '18 at 12:43





Is GSON giving you the time as a string in that format? (Where I learnt the AM/PM system, 12:30 AM is correct for half past midnight, BTW.)

– Ole V.V.
Nov 26 '18 at 12:43













Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

– Jean
Nov 26 '18 at 13:32





Yes it s a String format. and I d need all the hours between midnight and 1 am to be changed into 00:{the right minute}:00

– Jean
Nov 26 '18 at 13:32












2 Answers
2






active

oldest

votes


















1














I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:



    DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
String timeStringFromGson = "12:30:00 AM";
LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
String formattedTime = time.format(desiredTineFormatter);
System.out.println(formattedTime);


Output from this snippet is:




00:30:00 AM




The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.






share|improve this answer































    0














    Set your date format when creating gson object from Gson builder.



    Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();





    share|improve this answer


























    • I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

      – Jean
      Nov 26 '18 at 9:13






    • 1





      @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

      – JB Nizet
      Nov 26 '18 at 9:14













    • Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

      – htpvl
      Nov 26 '18 at 9:15











    • The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

      – Jean
      Nov 26 '18 at 9:53













    • How would this answer treat, say, 01:15:00 PM?

      – Ole V.V.
      Nov 26 '18 at 12:41












    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%2f53477714%2fjava-gson-date-converter-how-to-get-midnight-as-0000-am-instead-of-1200-am%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









    1














    I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:



        DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
    DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
    String timeStringFromGson = "12:30:00 AM";
    LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
    String formattedTime = time.format(desiredTineFormatter);
    System.out.println(formattedTime);


    Output from this snippet is:




    00:30:00 AM




    The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.






    share|improve this answer




























      1














      I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:



          DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
      DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
      String timeStringFromGson = "12:30:00 AM";
      LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
      String formattedTime = time.format(desiredTineFormatter);
      System.out.println(formattedTime);


      Output from this snippet is:




      00:30:00 AM




      The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.






      share|improve this answer


























        1












        1








        1







        I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:



            DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
        DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
        String timeStringFromGson = "12:30:00 AM";
        LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
        String formattedTime = time.format(desiredTineFormatter);
        System.out.println(formattedTime);


        Output from this snippet is:




        00:30:00 AM




        The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.






        share|improve this answer













        I would have preferred to persuade GSON to give me the desired format, but sorry, I don’t know GSON so cannot tell you whether that’s possible. However, if it isn’t, reformatting the string you get isn’t difficult when you know how:



            DateTimeFormatter gsonTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
        DateTimeFormatter desiredTineFormatter = DateTimeFormatter.ofPattern("KK:mm:ss a");
        String timeStringFromGson = "12:30:00 AM";
        LocalTime time = LocalTime.parse(timeStringFromGson, gsonTimeFormatter);
        String formattedTime = time.format(desiredTineFormatter);
        System.out.println(formattedTime);


        Output from this snippet is:




        00:30:00 AM




        The format pattern letters hh (lowercase) and KK (uppercase) both mean hour within AM or PM, but the former goes from 01 through 12, the latter from 00 through 11, so gives you the desired hour, also in case the hour from GSON happened to be 12.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 14:21









        Ole V.V.Ole V.V.

        32.1k74257




        32.1k74257

























            0














            Set your date format when creating gson object from Gson builder.



            Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();





            share|improve this answer


























            • I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

              – Jean
              Nov 26 '18 at 9:13






            • 1





              @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

              – JB Nizet
              Nov 26 '18 at 9:14













            • Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

              – htpvl
              Nov 26 '18 at 9:15











            • The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

              – Jean
              Nov 26 '18 at 9:53













            • How would this answer treat, say, 01:15:00 PM?

              – Ole V.V.
              Nov 26 '18 at 12:41
















            0














            Set your date format when creating gson object from Gson builder.



            Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();





            share|improve this answer


























            • I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

              – Jean
              Nov 26 '18 at 9:13






            • 1





              @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

              – JB Nizet
              Nov 26 '18 at 9:14













            • Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

              – htpvl
              Nov 26 '18 at 9:15











            • The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

              – Jean
              Nov 26 '18 at 9:53













            • How would this answer treat, say, 01:15:00 PM?

              – Ole V.V.
              Nov 26 '18 at 12:41














            0












            0








            0







            Set your date format when creating gson object from Gson builder.



            Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();





            share|improve this answer















            Set your date format when creating gson object from Gson builder.



            Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss").create();






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 9:14

























            answered Nov 26 '18 at 9:10









            htpvlhtpvl

            647310




            647310













            • I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

              – Jean
              Nov 26 '18 at 9:13






            • 1





              @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

              – JB Nizet
              Nov 26 '18 at 9:14













            • Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

              – htpvl
              Nov 26 '18 at 9:15











            • The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

              – Jean
              Nov 26 '18 at 9:53













            • How would this answer treat, say, 01:15:00 PM?

              – Ole V.V.
              Nov 26 '18 at 12:41



















            • I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

              – Jean
              Nov 26 '18 at 9:13






            • 1





              @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

              – JB Nizet
              Nov 26 '18 at 9:14













            • Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

              – htpvl
              Nov 26 '18 at 9:15











            • The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

              – Jean
              Nov 26 '18 at 9:53













            • How would this answer treat, say, 01:15:00 PM?

              – Ole V.V.
              Nov 26 '18 at 12:41

















            I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

            – Jean
            Nov 26 '18 at 9:13





            I think you didn't get my question. or may I didnt get your answer but I dont see how setDateFormat(HH:mm) would get me a 00:30:00 AM instead of a 12:30:00 AM ?

            – Jean
            Nov 26 '18 at 9:13




            1




            1





            @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

            – JB Nizet
            Nov 26 '18 at 9:14







            @Jean your question is begging for bad answers, since you didn't post any code allowing us to understand, reproduce and fix the problem. Post a complete minimal example reproducing the problem.

            – JB Nizet
            Nov 26 '18 at 9:14















            Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

            – htpvl
            Nov 26 '18 at 9:15





            Added ss for seconds. Just wonder why you need AM while the hour is in 00-23.

            – htpvl
            Nov 26 '18 at 9:15













            The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

            – Jean
            Nov 26 '18 at 9:53







            The original code is not mine. That s why I want to touch the Json generated as less as possible. The Json never sends 12-24 hours. But the app using this Json generates 00-24 hours.

            – Jean
            Nov 26 '18 at 9:53















            How would this answer treat, say, 01:15:00 PM?

            – Ole V.V.
            Nov 26 '18 at 12:41





            How would this answer treat, say, 01:15:00 PM?

            – Ole V.V.
            Nov 26 '18 at 12:41


















            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%2f53477714%2fjava-gson-date-converter-how-to-get-midnight-as-0000-am-instead-of-1200-am%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

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft