DateTime.plusMinutes(..) return minutes -1












0















I am encountering a problem using JodaTime. Here is my method:



public double calculateTimeDifference(String startedAt) {
DateTime cet = new DateTime(DateTimeZone.forID("CET"));

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

Period period = new Interval(cet, convertedStartedAt).toPeriod();
int minutes = period.getMinutes();

return minutes;
}


And I have a test that adds 30 minutes to the time:



@Test
public void testTimeDifference() {
DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);
testtime = testtime.plusMinutes(30);

// Format for input
DateTimeFormatter cet = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
// Parsing the date
DateTime requestedFormat = cet.parseDateTime(testtime.toString());
// Format for output
DateTimeFormatter requestedTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");

double time = carService.calculateTimeDifference(requestedTime.print(requestedFormat));

assertEquals(30, time, 0.0);
}


But when I run this the output is:



Expected : 30.0
Actual : 29.0









share|improve this question




















  • 2





    "Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

    – Andy Turner
    Nov 28 '18 at 10:35











  • Fixed it, sorry about that @AndyTurner

    – JowJoris
    Nov 28 '18 at 10:41








  • 1





    "DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

    – Andy Turner
    Nov 28 '18 at 10:42











  • For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

    – Andy Turner
    Nov 28 '18 at 10:43











  • Fixed the variable name also, was having some issues earlier with the timezones will try your answer

    – JowJoris
    Nov 28 '18 at 10:44
















0















I am encountering a problem using JodaTime. Here is my method:



public double calculateTimeDifference(String startedAt) {
DateTime cet = new DateTime(DateTimeZone.forID("CET"));

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

Period period = new Interval(cet, convertedStartedAt).toPeriod();
int minutes = period.getMinutes();

return minutes;
}


And I have a test that adds 30 minutes to the time:



@Test
public void testTimeDifference() {
DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);
testtime = testtime.plusMinutes(30);

// Format for input
DateTimeFormatter cet = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
// Parsing the date
DateTime requestedFormat = cet.parseDateTime(testtime.toString());
// Format for output
DateTimeFormatter requestedTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");

double time = carService.calculateTimeDifference(requestedTime.print(requestedFormat));

assertEquals(30, time, 0.0);
}


But when I run this the output is:



Expected : 30.0
Actual : 29.0









share|improve this question




















  • 2





    "Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

    – Andy Turner
    Nov 28 '18 at 10:35











  • Fixed it, sorry about that @AndyTurner

    – JowJoris
    Nov 28 '18 at 10:41








  • 1





    "DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

    – Andy Turner
    Nov 28 '18 at 10:42











  • For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

    – Andy Turner
    Nov 28 '18 at 10:43











  • Fixed the variable name also, was having some issues earlier with the timezones will try your answer

    – JowJoris
    Nov 28 '18 at 10:44














0












0








0








I am encountering a problem using JodaTime. Here is my method:



public double calculateTimeDifference(String startedAt) {
DateTime cet = new DateTime(DateTimeZone.forID("CET"));

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

Period period = new Interval(cet, convertedStartedAt).toPeriod();
int minutes = period.getMinutes();

return minutes;
}


And I have a test that adds 30 minutes to the time:



@Test
public void testTimeDifference() {
DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);
testtime = testtime.plusMinutes(30);

// Format for input
DateTimeFormatter cet = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
// Parsing the date
DateTime requestedFormat = cet.parseDateTime(testtime.toString());
// Format for output
DateTimeFormatter requestedTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");

double time = carService.calculateTimeDifference(requestedTime.print(requestedFormat));

assertEquals(30, time, 0.0);
}


But when I run this the output is:



Expected : 30.0
Actual : 29.0









share|improve this question
















I am encountering a problem using JodaTime. Here is my method:



public double calculateTimeDifference(String startedAt) {
DateTime cet = new DateTime(DateTimeZone.forID("CET"));

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

Period period = new Interval(cet, convertedStartedAt).toPeriod();
int minutes = period.getMinutes();

return minutes;
}


And I have a test that adds 30 minutes to the time:



@Test
public void testTimeDifference() {
DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);
testtime = testtime.plusMinutes(30);

// Format for input
DateTimeFormatter cet = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
// Parsing the date
DateTime requestedFormat = cet.parseDateTime(testtime.toString());
// Format for output
DateTimeFormatter requestedTime = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");

double time = carService.calculateTimeDifference(requestedTime.print(requestedFormat));

assertEquals(30, time, 0.0);
}


But when I run this the output is:



Expected : 30.0
Actual : 29.0






java jodatime






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 23:25









halfer

14.7k759116




14.7k759116










asked Nov 28 '18 at 10:29









JowJorisJowJoris

43




43








  • 2





    "Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

    – Andy Turner
    Nov 28 '18 at 10:35











  • Fixed it, sorry about that @AndyTurner

    – JowJoris
    Nov 28 '18 at 10:41








  • 1





    "DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

    – Andy Turner
    Nov 28 '18 at 10:42











  • For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

    – Andy Turner
    Nov 28 '18 at 10:43











  • Fixed the variable name also, was having some issues earlier with the timezones will try your answer

    – JowJoris
    Nov 28 '18 at 10:44














  • 2





    "Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

    – Andy Turner
    Nov 28 '18 at 10:35











  • Fixed it, sorry about that @AndyTurner

    – JowJoris
    Nov 28 '18 at 10:41








  • 1





    "DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

    – Andy Turner
    Nov 28 '18 at 10:42











  • For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

    – Andy Turner
    Nov 28 '18 at 10:43











  • Fixed the variable name also, was having some issues earlier with the timezones will try your answer

    – JowJoris
    Nov 28 '18 at 10:44








2




2





"Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

– Andy Turner
Nov 28 '18 at 10:35





"Here is my method" Is this carService.calculateTimeDifference? Please show an Minimal, Complete, and Verifiable example, rather than making us guess how these fit together.

– Andy Turner
Nov 28 '18 at 10:35













Fixed it, sorry about that @AndyTurner

– JowJoris
Nov 28 '18 at 10:41







Fixed it, sorry about that @AndyTurner

– JowJoris
Nov 28 '18 at 10:41






1




1





"DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

– Andy Turner
Nov 28 '18 at 10:42





"DateTime utc = new DateTime(DateTimeZone.forID("CET"));" Well, that's a confusing name :) In general, I would say that you're doing too much conversion here: date times to strings to date times to intervals to periods. There is bound to be some loss of precision in there.

– Andy Turner
Nov 28 '18 at 10:42













For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

– Andy Turner
Nov 28 '18 at 10:43





For example, why new Interval(utc, convertedStartedAt).toPeriod(), rather than new Period(utc, convertedStartedAt)?

– Andy Turner
Nov 28 '18 at 10:43













Fixed the variable name also, was having some issues earlier with the timezones will try your answer

– JowJoris
Nov 28 '18 at 10:44





Fixed the variable name also, was having some issues earlier with the timezones will try your answer

– JowJoris
Nov 28 '18 at 10:44












2 Answers
2






active

oldest

votes


















3














The reason is that you generate twice current date and time:
once here:



    DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);


and once here:



    DateTime utc = new DateTime(DateTimeZone.forID("CET"));


So the actual difference between is 29 minutes 59 seconds and 360 millis (in my test), rounded to 29 minutes.






share|improve this answer































    0














    Okay I fixed it now, thanks to the comment of @Benoit. I changed the method calculateTimeRemaining to this:



    public double calculateTimeDifference(String startedAt) {
    DateTime utc = new DateTime(DateTimeZone.forID("CET"));

    DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
    DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

    Period period = new Period(utc, convertedStartedAt);
    double hours = period.getHours();
    double minutes = period.getMinutes();

    return hours + Math.round(minutes * 10.0/ 60)/10.0;
    }


    Thanks to all of you!






    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%2f53517298%2fdatetime-plusminutes-return-minutes-1%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









      3














      The reason is that you generate twice current date and time:
      once here:



          DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);


      and once here:



          DateTime utc = new DateTime(DateTimeZone.forID("CET"));


      So the actual difference between is 29 minutes 59 seconds and 360 millis (in my test), rounded to 29 minutes.






      share|improve this answer




























        3














        The reason is that you generate twice current date and time:
        once here:



            DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);


        and once here:



            DateTime utc = new DateTime(DateTimeZone.forID("CET"));


        So the actual difference between is 29 minutes 59 seconds and 360 millis (in my test), rounded to 29 minutes.






        share|improve this answer


























          3












          3








          3







          The reason is that you generate twice current date and time:
          once here:



              DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);


          and once here:



              DateTime utc = new DateTime(DateTimeZone.forID("CET"));


          So the actual difference between is 29 minutes 59 seconds and 360 millis (in my test), rounded to 29 minutes.






          share|improve this answer













          The reason is that you generate twice current date and time:
          once here:



              DateTime testtime = new DateTime(DateTimeZone.forID("CET")).plusHours(2);


          and once here:



              DateTime utc = new DateTime(DateTimeZone.forID("CET"));


          So the actual difference between is 29 minutes 59 seconds and 360 millis (in my test), rounded to 29 minutes.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 10:44









          BenoitBenoit

          2,5462929




          2,5462929

























              0














              Okay I fixed it now, thanks to the comment of @Benoit. I changed the method calculateTimeRemaining to this:



              public double calculateTimeDifference(String startedAt) {
              DateTime utc = new DateTime(DateTimeZone.forID("CET"));

              DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
              DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

              Period period = new Period(utc, convertedStartedAt);
              double hours = period.getHours();
              double minutes = period.getMinutes();

              return hours + Math.round(minutes * 10.0/ 60)/10.0;
              }


              Thanks to all of you!






              share|improve this answer




























                0














                Okay I fixed it now, thanks to the comment of @Benoit. I changed the method calculateTimeRemaining to this:



                public double calculateTimeDifference(String startedAt) {
                DateTime utc = new DateTime(DateTimeZone.forID("CET"));

                DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
                DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

                Period period = new Period(utc, convertedStartedAt);
                double hours = period.getHours();
                double minutes = period.getMinutes();

                return hours + Math.round(minutes * 10.0/ 60)/10.0;
                }


                Thanks to all of you!






                share|improve this answer


























                  0












                  0








                  0







                  Okay I fixed it now, thanks to the comment of @Benoit. I changed the method calculateTimeRemaining to this:



                  public double calculateTimeDifference(String startedAt) {
                  DateTime utc = new DateTime(DateTimeZone.forID("CET"));

                  DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
                  DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

                  Period period = new Period(utc, convertedStartedAt);
                  double hours = period.getHours();
                  double minutes = period.getMinutes();

                  return hours + Math.round(minutes * 10.0/ 60)/10.0;
                  }


                  Thanks to all of you!






                  share|improve this answer













                  Okay I fixed it now, thanks to the comment of @Benoit. I changed the method calculateTimeRemaining to this:



                  public double calculateTimeDifference(String startedAt) {
                  DateTime utc = new DateTime(DateTimeZone.forID("CET"));

                  DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss'Z'");
                  DateTime convertedStartedAt = formatter.parseDateTime(startedAt);

                  Period period = new Period(utc, convertedStartedAt);
                  double hours = period.getHours();
                  double minutes = period.getMinutes();

                  return hours + Math.round(minutes * 10.0/ 60)/10.0;
                  }


                  Thanks to all of you!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 28 '18 at 11:14









                  JowJorisJowJoris

                  43




                  43






























                      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%2f53517298%2fdatetime-plusminutes-return-minutes-1%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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)