What's the difference between these two ways of creating a DateTime from a LocalDateTime?Differences between HashMap and Hashtable?Create ArrayList from arrayWhat is the difference between public, protected, package-private and private in Java?What's the difference between SoftReference and WeakReference in Java?Difference between StringBuilder and StringBufferWhat's the simplest way to print a Java array?Difference between wait() and sleep()What's the difference between @Component, @Repository & @Service annotations in Spring?What's the difference between JPA and Hibernate?How to Instantiate Joda DateTime from TimeZone String ID and Epoch
Do pedestrians imitate automotive traffic?
A Real World Example for Divide and Conquer Method
Applying for jobs when I have an obvious scar
Why are flying carpets banned while flying brooms are not?
Have any lunar probes used a cold shield?
Weight functions in graph algorithms
"move up the school" meaning
How is it possible for the induced emf to take negative values in Faraday's Law of induction?
Inside Out and Back to Front
What does Windows' "Tuning up Application Start" do?
Does any picture file format embed author, title etc.?
Does unblocking power bar outlets through short extension cords increase fire risk?
What is the minimum wait before I may I re-enter the USA after a 90 day visit on the Visa B-2 Program?
Aren't all schwa sounds literally /ø/?
"This used to be my phone number"
Do I have to mention my main characters age?
Conditional statement in a function for PS1 are not re-evalutated
Is it ethical to tell my teaching assistant that I like him?
Why should fork() have been designed to return a file descriptor?
Counting multiples of 3 up to a given number
Has anyone ever written a novel or short story composed of only dialogue?
Making an example from 'Clean Code' more functional
What is the origin of "Wonder begets wisdom?"
Trivial non-dark twist in dark fantasy
What's the difference between these two ways of creating a DateTime from a LocalDateTime?
Differences between HashMap and Hashtable?Create ArrayList from arrayWhat is the difference between public, protected, package-private and private in Java?What's the difference between SoftReference and WeakReference in Java?Difference between StringBuilder and StringBufferWhat's the simplest way to print a Java array?Difference between wait() and sleep()What's the difference between @Component, @Repository & @Service annotations in Spring?What's the difference between JPA and Hibernate?How to Instantiate Joda DateTime from TimeZone String ID and Epoch
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Our application uses jodatime to handle times, and (for API formatting reasons) we store times in a model class which looks a bit like this:
class Event
private LocalDateTime localTime;
private DateTimeZone timeZone;
public DateTime getTime()
return localStopTime.toDateTime(timeZone);
public void setTime(DateTime value)
this.localTime = value.toLocalDateTime();
this.timeZone = value.getZone();
// ...more boilerplate
Further downstream I noticed we were getting a different time out than we were setting. I figured we were converting the fields back to a DateTime wrong, since the local fields seem to have the right values.
On a whim I tried changing the getter and now it works, but I have no idea why:
public DateTime getTime()
return localStopTime.toDateTime().withZone(timeZone);
The joda documentation is a bit tight-lipped about how it carries out the toDateTime()
call; it says it "uses" a certain timezone somehow but that's it.
Can anyone explain to me what the difference is between
return localStopTime.toDateTime(timeZone);
and
return localStopTime.toDateTime().withZone(timeZone);
?
Thanks in advance!
Edit: I've figured it out - I was using "Etc/GMT" as my time zone and that didn't take into account daylight savings. Have marked Marco's answer as correct
java time timezone jodatime
add a comment |
Our application uses jodatime to handle times, and (for API formatting reasons) we store times in a model class which looks a bit like this:
class Event
private LocalDateTime localTime;
private DateTimeZone timeZone;
public DateTime getTime()
return localStopTime.toDateTime(timeZone);
public void setTime(DateTime value)
this.localTime = value.toLocalDateTime();
this.timeZone = value.getZone();
// ...more boilerplate
Further downstream I noticed we were getting a different time out than we were setting. I figured we were converting the fields back to a DateTime wrong, since the local fields seem to have the right values.
On a whim I tried changing the getter and now it works, but I have no idea why:
public DateTime getTime()
return localStopTime.toDateTime().withZone(timeZone);
The joda documentation is a bit tight-lipped about how it carries out the toDateTime()
call; it says it "uses" a certain timezone somehow but that's it.
Can anyone explain to me what the difference is between
return localStopTime.toDateTime(timeZone);
and
return localStopTime.toDateTime().withZone(timeZone);
?
Thanks in advance!
Edit: I've figured it out - I was using "Etc/GMT" as my time zone and that didn't take into account daylight savings. Have marked Marco's answer as correct
java time timezone jodatime
2
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
My observation is the opposite as yours. WIthreturn localStopTime.toDateTime(timeZone);
I do get the sameDateTime
back. WIthreturn localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the providedDateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.
– Ole V.V.
Mar 27 at 11:06
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
I used the current time in a foreign time zone:new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.
– Ole V.V.
Mar 27 at 17:16
(1) DateTime I set:2019-03-28T01:28:26.755+08:00
. DateTime I got fromreturn localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set:2019-03-28T01:29:40.414+08:00
. DateTime I got fromreturn localStopTime.toDateTime().withZone(timeZone);
:2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.
– Ole V.V.
Mar 27 at 17:30
add a comment |
Our application uses jodatime to handle times, and (for API formatting reasons) we store times in a model class which looks a bit like this:
class Event
private LocalDateTime localTime;
private DateTimeZone timeZone;
public DateTime getTime()
return localStopTime.toDateTime(timeZone);
public void setTime(DateTime value)
this.localTime = value.toLocalDateTime();
this.timeZone = value.getZone();
// ...more boilerplate
Further downstream I noticed we were getting a different time out than we were setting. I figured we were converting the fields back to a DateTime wrong, since the local fields seem to have the right values.
On a whim I tried changing the getter and now it works, but I have no idea why:
public DateTime getTime()
return localStopTime.toDateTime().withZone(timeZone);
The joda documentation is a bit tight-lipped about how it carries out the toDateTime()
call; it says it "uses" a certain timezone somehow but that's it.
Can anyone explain to me what the difference is between
return localStopTime.toDateTime(timeZone);
and
return localStopTime.toDateTime().withZone(timeZone);
?
Thanks in advance!
Edit: I've figured it out - I was using "Etc/GMT" as my time zone and that didn't take into account daylight savings. Have marked Marco's answer as correct
java time timezone jodatime
Our application uses jodatime to handle times, and (for API formatting reasons) we store times in a model class which looks a bit like this:
class Event
private LocalDateTime localTime;
private DateTimeZone timeZone;
public DateTime getTime()
return localStopTime.toDateTime(timeZone);
public void setTime(DateTime value)
this.localTime = value.toLocalDateTime();
this.timeZone = value.getZone();
// ...more boilerplate
Further downstream I noticed we were getting a different time out than we were setting. I figured we were converting the fields back to a DateTime wrong, since the local fields seem to have the right values.
On a whim I tried changing the getter and now it works, but I have no idea why:
public DateTime getTime()
return localStopTime.toDateTime().withZone(timeZone);
The joda documentation is a bit tight-lipped about how it carries out the toDateTime()
call; it says it "uses" a certain timezone somehow but that's it.
Can anyone explain to me what the difference is between
return localStopTime.toDateTime(timeZone);
and
return localStopTime.toDateTime().withZone(timeZone);
?
Thanks in advance!
Edit: I've figured it out - I was using "Etc/GMT" as my time zone and that didn't take into account daylight savings. Have marked Marco's answer as correct
java time timezone jodatime
java time timezone jodatime
edited Mar 27 at 17:34
Binney
asked Mar 26 at 12:20
BinneyBinney
1231 silver badge5 bronze badges
1231 silver badge5 bronze badges
2
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
My observation is the opposite as yours. WIthreturn localStopTime.toDateTime(timeZone);
I do get the sameDateTime
back. WIthreturn localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the providedDateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.
– Ole V.V.
Mar 27 at 11:06
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
I used the current time in a foreign time zone:new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.
– Ole V.V.
Mar 27 at 17:16
(1) DateTime I set:2019-03-28T01:28:26.755+08:00
. DateTime I got fromreturn localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set:2019-03-28T01:29:40.414+08:00
. DateTime I got fromreturn localStopTime.toDateTime().withZone(timeZone);
:2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.
– Ole V.V.
Mar 27 at 17:30
add a comment |
2
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
My observation is the opposite as yours. WIthreturn localStopTime.toDateTime(timeZone);
I do get the sameDateTime
back. WIthreturn localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the providedDateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.
– Ole V.V.
Mar 27 at 11:06
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
I used the current time in a foreign time zone:new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.
– Ole V.V.
Mar 27 at 17:16
(1) DateTime I set:2019-03-28T01:28:26.755+08:00
. DateTime I got fromreturn localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set:2019-03-28T01:29:40.414+08:00
. DateTime I got fromreturn localStopTime.toDateTime().withZone(timeZone);
:2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.
– Ole V.V.
Mar 27 at 17:30
2
2
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
My observation is the opposite as yours. WIth
return localStopTime.toDateTime(timeZone);
I do get the same DateTime
back. WIth return localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the provided DateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.– Ole V.V.
Mar 27 at 11:06
My observation is the opposite as yours. WIth
return localStopTime.toDateTime(timeZone);
I do get the same DateTime
back. WIth return localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the provided DateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.– Ole V.V.
Mar 27 at 11:06
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
I used the current time in a foreign time zone:
new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.– Ole V.V.
Mar 27 at 17:16
I used the current time in a foreign time zone:
new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.– Ole V.V.
Mar 27 at 17:16
(1) DateTime I set:
2019-03-28T01:28:26.755+08:00
. DateTime I got from return localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set: 2019-03-28T01:29:40.414+08:00
. DateTime I got from return localStopTime.toDateTime().withZone(timeZone);
: 2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.– Ole V.V.
Mar 27 at 17:30
(1) DateTime I set:
2019-03-28T01:28:26.755+08:00
. DateTime I got from return localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set: 2019-03-28T01:29:40.414+08:00
. DateTime I got from return localStopTime.toDateTime().withZone(timeZone);
: 2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.– Ole V.V.
Mar 27 at 17:30
add a comment |
1 Answer
1
active
oldest
votes
The difference between those two is the next one, you use withZone()
to: (as JavaDocs says)
Returns a copy of this datetime with a different time zone, preserving the millisecond instant.
Also, the JavaDocs provides a good example:
This method is useful for finding the local time in another timezone.
For example, if this instant holds 12:30 in Europe/London, the result
from this method with Europe/Paris would be 13:30.
And you use the toDateTime(timeZone)
to return a DateTime
object but applying the specified timeZone
to it.
So, you can use toDateTime(timeZone).withZone(secondTimeZone)
and you will get a copy of the DateTime
generated by the first statement (toDateTime(timeZone)
) but, with a different time zone, perseving the milisecond instant. And if you use toDateTime()
with no parameters, will only retrieve a DateTime
object.
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%2f55357020%2fwhats-the-difference-between-these-two-ways-of-creating-a-datetime-from-a-local%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The difference between those two is the next one, you use withZone()
to: (as JavaDocs says)
Returns a copy of this datetime with a different time zone, preserving the millisecond instant.
Also, the JavaDocs provides a good example:
This method is useful for finding the local time in another timezone.
For example, if this instant holds 12:30 in Europe/London, the result
from this method with Europe/Paris would be 13:30.
And you use the toDateTime(timeZone)
to return a DateTime
object but applying the specified timeZone
to it.
So, you can use toDateTime(timeZone).withZone(secondTimeZone)
and you will get a copy of the DateTime
generated by the first statement (toDateTime(timeZone)
) but, with a different time zone, perseving the milisecond instant. And if you use toDateTime()
with no parameters, will only retrieve a DateTime
object.
add a comment |
The difference between those two is the next one, you use withZone()
to: (as JavaDocs says)
Returns a copy of this datetime with a different time zone, preserving the millisecond instant.
Also, the JavaDocs provides a good example:
This method is useful for finding the local time in another timezone.
For example, if this instant holds 12:30 in Europe/London, the result
from this method with Europe/Paris would be 13:30.
And you use the toDateTime(timeZone)
to return a DateTime
object but applying the specified timeZone
to it.
So, you can use toDateTime(timeZone).withZone(secondTimeZone)
and you will get a copy of the DateTime
generated by the first statement (toDateTime(timeZone)
) but, with a different time zone, perseving the milisecond instant. And if you use toDateTime()
with no parameters, will only retrieve a DateTime
object.
add a comment |
The difference between those two is the next one, you use withZone()
to: (as JavaDocs says)
Returns a copy of this datetime with a different time zone, preserving the millisecond instant.
Also, the JavaDocs provides a good example:
This method is useful for finding the local time in another timezone.
For example, if this instant holds 12:30 in Europe/London, the result
from this method with Europe/Paris would be 13:30.
And you use the toDateTime(timeZone)
to return a DateTime
object but applying the specified timeZone
to it.
So, you can use toDateTime(timeZone).withZone(secondTimeZone)
and you will get a copy of the DateTime
generated by the first statement (toDateTime(timeZone)
) but, with a different time zone, perseving the milisecond instant. And if you use toDateTime()
with no parameters, will only retrieve a DateTime
object.
The difference between those two is the next one, you use withZone()
to: (as JavaDocs says)
Returns a copy of this datetime with a different time zone, preserving the millisecond instant.
Also, the JavaDocs provides a good example:
This method is useful for finding the local time in another timezone.
For example, if this instant holds 12:30 in Europe/London, the result
from this method with Europe/Paris would be 13:30.
And you use the toDateTime(timeZone)
to return a DateTime
object but applying the specified timeZone
to it.
So, you can use toDateTime(timeZone).withZone(secondTimeZone)
and you will get a copy of the DateTime
generated by the first statement (toDateTime(timeZone)
) but, with a different time zone, perseving the milisecond instant. And if you use toDateTime()
with no parameters, will only retrieve a DateTime
object.
answered Mar 26 at 12:47
Marco MarchettiMarco Marchetti
832 silver badges10 bronze badges
832 silver badges10 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55357020%2fwhats-the-difference-between-these-two-ways-of-creating-a-datetime-from-a-local%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
jodatime is open source! Take a look at the sources of LocalDateTime:getDateTime() and DateTime:withZone() to exactly see what is going on.
– lupz
Mar 26 at 12:42
My observation is the opposite as yours. WIth
return localStopTime.toDateTime(timeZone);
I do get the sameDateTime
back. WIthreturn localStopTime.toDateTime().withZone(timeZone);
I get a different one (unless the zone of the providedDateTime
is my default time zone). What I am observing also agrees with my understanding of the intended behaviour.– Ole V.V.
Mar 27 at 11:06
@OleV.V. - that's fascinating - can I ask what time you're using? I'm testing with localTime=2014-04-24T15:55:00.000, timeZone=Etc/GMT i.e. during daylight savings time.
– Binney
Mar 27 at 17:10
I used the current time in a foreign time zone:
new DateTime(DateTimeZone.forID("Asia/Shanghai"))
(my own time zone is Europe/Copenhagen). I suspect that there may be something unplanned going on on your side, but I cannot guess what.– Ole V.V.
Mar 27 at 17:16
(1) DateTime I set:
2019-03-28T01:28:26.755+08:00
. DateTime I got fromreturn localStopTime.toDateTime(timeZone);
: the same. (2) DateTime I set:2019-03-28T01:29:40.414+08:00
. DateTime I got fromreturn localStopTime.toDateTime().withZone(timeZone);
:2019-03-28T08:29:40.414+08:00
. Note that the hours are 08 instead of 01.– Ole V.V.
Mar 27 at 17:30