DateTime.plusMinutes(..) return minutes -1
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
|
show 1 more comment
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
2
"Here is my method" Is thiscarService.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, whynew Interval(utc, convertedStartedAt).toPeriod()
, rather thannew 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
|
show 1 more comment
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
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
java jodatime
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 thiscarService.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, whynew Interval(utc, convertedStartedAt).toPeriod()
, rather thannew 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
|
show 1 more comment
2
"Here is my method" Is thiscarService.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, whynew Interval(utc, convertedStartedAt).toPeriod()
, rather thannew 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
|
show 1 more comment
2 Answers
2
active
oldest
votes
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.
add a comment |
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!
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 28 '18 at 10:44
BenoitBenoit
2,5462929
2,5462929
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 28 '18 at 11:14
JowJorisJowJoris
43
43
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thannew 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