Parse date time string to Apex DateTime

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












How do I parse




2018-10-01T00:00:00Z




Into an instance of DateTime



I've tried:



DateTime.valueOfGmt('2018-10-01T00:00:00Z');
DateTime.parse('2018-10-01T00:00:00Z');
DateTime.valueOf('2018-10-01T00:00:00Z');
DateTime.valueOfGmt('2018-10-01T00:00:00Z');


And I always get error:




ERROR: System.TypeException: Invalid date/time: 2018-10-01T00:00:00Z











share|improve this question





























    up vote
    2
    down vote

    favorite












    How do I parse




    2018-10-01T00:00:00Z




    Into an instance of DateTime



    I've tried:



    DateTime.valueOfGmt('2018-10-01T00:00:00Z');
    DateTime.parse('2018-10-01T00:00:00Z');
    DateTime.valueOf('2018-10-01T00:00:00Z');
    DateTime.valueOfGmt('2018-10-01T00:00:00Z');


    And I always get error:




    ERROR: System.TypeException: Invalid date/time: 2018-10-01T00:00:00Z











    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      How do I parse




      2018-10-01T00:00:00Z




      Into an instance of DateTime



      I've tried:



      DateTime.valueOfGmt('2018-10-01T00:00:00Z');
      DateTime.parse('2018-10-01T00:00:00Z');
      DateTime.valueOf('2018-10-01T00:00:00Z');
      DateTime.valueOfGmt('2018-10-01T00:00:00Z');


      And I always get error:




      ERROR: System.TypeException: Invalid date/time: 2018-10-01T00:00:00Z











      share|improve this question















      How do I parse




      2018-10-01T00:00:00Z




      Into an instance of DateTime



      I've tried:



      DateTime.valueOfGmt('2018-10-01T00:00:00Z');
      DateTime.parse('2018-10-01T00:00:00Z');
      DateTime.valueOf('2018-10-01T00:00:00Z');
      DateTime.valueOfGmt('2018-10-01T00:00:00Z');


      And I always get error:




      ERROR: System.TypeException: Invalid date/time: 2018-10-01T00:00:00Z








      apex datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 mins ago

























      asked 1 hour ago









      Robs

      1,195424




      1,195424




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);


          Note the extra set of double quotes - they allow the JSON parser to interpret that value.






          share|improve this answer
















          • 1




            +1 Took the characters right out of my text box. 😄
            – sfdcfox
            1 hour ago

















          up vote
          2
          down vote













          As an alternative, you can use a regular expression to get the values into a more digestible form:



          Pattern dt = Pattern.compile('(?i)(\d4)-(\d2)-(\d2)T(\d2):(\d2):(\d2)Z');
          String exampleTime = '2018-10-01T00:00:00z';
          Matcher m = dt.matcher(exampletime);
          DateTime result;
          if(m.find())
          result = DateTime.newInstanceGMT(
          Integer.valueOf(m.group(1)),
          Integer.valueOf(m.group(2)),
          Integer.valueOf(m.group(3)),
          Integer.valueOf(m.group(4)),
          Integer.valueOf(m.group(5)),
          Integer.valueOf(m.group(6))
          );

          system.debug(result);





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235806%2fparse-date-time-string-to-apex-datetime%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote



            accepted










            JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);


            Note the extra set of double quotes - they allow the JSON parser to interpret that value.






            share|improve this answer
















            • 1




              +1 Took the characters right out of my text box. 😄
              – sfdcfox
              1 hour ago














            up vote
            5
            down vote



            accepted










            JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);


            Note the extra set of double quotes - they allow the JSON parser to interpret that value.






            share|improve this answer
















            • 1




              +1 Took the characters right out of my text box. 😄
              – sfdcfox
              1 hour ago












            up vote
            5
            down vote



            accepted







            up vote
            5
            down vote



            accepted






            JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);


            Note the extra set of double quotes - they allow the JSON parser to interpret that value.






            share|improve this answer












            JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class);


            Note the extra set of double quotes - they allow the JSON parser to interpret that value.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            David Reed

            21.5k31640




            21.5k31640







            • 1




              +1 Took the characters right out of my text box. 😄
              – sfdcfox
              1 hour ago












            • 1




              +1 Took the characters right out of my text box. 😄
              – sfdcfox
              1 hour ago







            1




            1




            +1 Took the characters right out of my text box. 😄
            – sfdcfox
            1 hour ago




            +1 Took the characters right out of my text box. 😄
            – sfdcfox
            1 hour ago












            up vote
            2
            down vote













            As an alternative, you can use a regular expression to get the values into a more digestible form:



            Pattern dt = Pattern.compile('(?i)(\d4)-(\d2)-(\d2)T(\d2):(\d2):(\d2)Z');
            String exampleTime = '2018-10-01T00:00:00z';
            Matcher m = dt.matcher(exampletime);
            DateTime result;
            if(m.find())
            result = DateTime.newInstanceGMT(
            Integer.valueOf(m.group(1)),
            Integer.valueOf(m.group(2)),
            Integer.valueOf(m.group(3)),
            Integer.valueOf(m.group(4)),
            Integer.valueOf(m.group(5)),
            Integer.valueOf(m.group(6))
            );

            system.debug(result);





            share|improve this answer
























              up vote
              2
              down vote













              As an alternative, you can use a regular expression to get the values into a more digestible form:



              Pattern dt = Pattern.compile('(?i)(\d4)-(\d2)-(\d2)T(\d2):(\d2):(\d2)Z');
              String exampleTime = '2018-10-01T00:00:00z';
              Matcher m = dt.matcher(exampletime);
              DateTime result;
              if(m.find())
              result = DateTime.newInstanceGMT(
              Integer.valueOf(m.group(1)),
              Integer.valueOf(m.group(2)),
              Integer.valueOf(m.group(3)),
              Integer.valueOf(m.group(4)),
              Integer.valueOf(m.group(5)),
              Integer.valueOf(m.group(6))
              );

              system.debug(result);





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                As an alternative, you can use a regular expression to get the values into a more digestible form:



                Pattern dt = Pattern.compile('(?i)(\d4)-(\d2)-(\d2)T(\d2):(\d2):(\d2)Z');
                String exampleTime = '2018-10-01T00:00:00z';
                Matcher m = dt.matcher(exampletime);
                DateTime result;
                if(m.find())
                result = DateTime.newInstanceGMT(
                Integer.valueOf(m.group(1)),
                Integer.valueOf(m.group(2)),
                Integer.valueOf(m.group(3)),
                Integer.valueOf(m.group(4)),
                Integer.valueOf(m.group(5)),
                Integer.valueOf(m.group(6))
                );

                system.debug(result);





                share|improve this answer












                As an alternative, you can use a regular expression to get the values into a more digestible form:



                Pattern dt = Pattern.compile('(?i)(\d4)-(\d2)-(\d2)T(\d2):(\d2):(\d2)Z');
                String exampleTime = '2018-10-01T00:00:00z';
                Matcher m = dt.matcher(exampletime);
                DateTime result;
                if(m.find())
                result = DateTime.newInstanceGMT(
                Integer.valueOf(m.group(1)),
                Integer.valueOf(m.group(2)),
                Integer.valueOf(m.group(3)),
                Integer.valueOf(m.group(4)),
                Integer.valueOf(m.group(5)),
                Integer.valueOf(m.group(6))
                );

                system.debug(result);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                sfdcfox

                232k10179395




                232k10179395



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f235806%2fparse-date-time-string-to-apex-datetime%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    What does second last employer means? [closed]

                    One-line joke