xmlgregoriancalendar hashcode does not handle milliseconds












1















I am trying to hash my object:



protected XMLGregorianCalendar startTime;


value of getStartTime() is 2018-06-21T12:04:10.000Z



The hashCode method at the bottom from XMLGregorianCalendar does not handle milliseconds, so I will never get a unique hashCode even when I use a startTime with different value for milliseconds like 2018-06-21T12:04:10.111Z



Both 2018-06-21T12:04:10.000Z and 2018-06-21T12:04:10.111Z result in the same hashCode unless I change the seconds, minutes, hour, etc etc. I need it to also take into account the milliseconds.



Is there a different hashCode() method that will handle this? How can I accomplish this?



   public int hashCode() {

// Following two dates compare to EQUALS since in different timezones.
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
//
// Must ensure both instances generate same hashcode by normalizing
// this to UTC timezone.
int timezone = getTimezone();
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
timezone = 0;
}
XMLGregorianCalendar gc = this;
if (timezone != 0) {
gc = this.normalize();
}
return gc.getYear()
+ gc.getMonth()
+ gc.getDay()
+ gc.getHour()
+ gc.getMinute()
+ gc.getSecond(); //Where's milliseconds???
}









share|improve this question

























  • Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

    – Vadim
    Nov 21 '18 at 16:24











  • @Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

    – Joey Corkey
    Nov 21 '18 at 16:53






  • 2





    1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

    – Vadim
    Nov 21 '18 at 17:59






  • 1





    In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

    – Ole V.V.
    Nov 26 '18 at 17:17






  • 1





    Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

    – Ole V.V.
    Nov 27 '18 at 11:13
















1















I am trying to hash my object:



protected XMLGregorianCalendar startTime;


value of getStartTime() is 2018-06-21T12:04:10.000Z



The hashCode method at the bottom from XMLGregorianCalendar does not handle milliseconds, so I will never get a unique hashCode even when I use a startTime with different value for milliseconds like 2018-06-21T12:04:10.111Z



Both 2018-06-21T12:04:10.000Z and 2018-06-21T12:04:10.111Z result in the same hashCode unless I change the seconds, minutes, hour, etc etc. I need it to also take into account the milliseconds.



Is there a different hashCode() method that will handle this? How can I accomplish this?



   public int hashCode() {

// Following two dates compare to EQUALS since in different timezones.
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
//
// Must ensure both instances generate same hashcode by normalizing
// this to UTC timezone.
int timezone = getTimezone();
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
timezone = 0;
}
XMLGregorianCalendar gc = this;
if (timezone != 0) {
gc = this.normalize();
}
return gc.getYear()
+ gc.getMonth()
+ gc.getDay()
+ gc.getHour()
+ gc.getMinute()
+ gc.getSecond(); //Where's milliseconds???
}









share|improve this question

























  • Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

    – Vadim
    Nov 21 '18 at 16:24











  • @Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

    – Joey Corkey
    Nov 21 '18 at 16:53






  • 2





    1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

    – Vadim
    Nov 21 '18 at 17:59






  • 1





    In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

    – Ole V.V.
    Nov 26 '18 at 17:17






  • 1





    Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

    – Ole V.V.
    Nov 27 '18 at 11:13














1












1








1








I am trying to hash my object:



protected XMLGregorianCalendar startTime;


value of getStartTime() is 2018-06-21T12:04:10.000Z



The hashCode method at the bottom from XMLGregorianCalendar does not handle milliseconds, so I will never get a unique hashCode even when I use a startTime with different value for milliseconds like 2018-06-21T12:04:10.111Z



Both 2018-06-21T12:04:10.000Z and 2018-06-21T12:04:10.111Z result in the same hashCode unless I change the seconds, minutes, hour, etc etc. I need it to also take into account the milliseconds.



Is there a different hashCode() method that will handle this? How can I accomplish this?



   public int hashCode() {

// Following two dates compare to EQUALS since in different timezones.
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
//
// Must ensure both instances generate same hashcode by normalizing
// this to UTC timezone.
int timezone = getTimezone();
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
timezone = 0;
}
XMLGregorianCalendar gc = this;
if (timezone != 0) {
gc = this.normalize();
}
return gc.getYear()
+ gc.getMonth()
+ gc.getDay()
+ gc.getHour()
+ gc.getMinute()
+ gc.getSecond(); //Where's milliseconds???
}









share|improve this question
















I am trying to hash my object:



protected XMLGregorianCalendar startTime;


value of getStartTime() is 2018-06-21T12:04:10.000Z



The hashCode method at the bottom from XMLGregorianCalendar does not handle milliseconds, so I will never get a unique hashCode even when I use a startTime with different value for milliseconds like 2018-06-21T12:04:10.111Z



Both 2018-06-21T12:04:10.000Z and 2018-06-21T12:04:10.111Z result in the same hashCode unless I change the seconds, minutes, hour, etc etc. I need it to also take into account the milliseconds.



Is there a different hashCode() method that will handle this? How can I accomplish this?



   public int hashCode() {

// Following two dates compare to EQUALS since in different timezones.
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
//
// Must ensure both instances generate same hashcode by normalizing
// this to UTC timezone.
int timezone = getTimezone();
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
timezone = 0;
}
XMLGregorianCalendar gc = this;
if (timezone != 0) {
gc = this.normalize();
}
return gc.getYear()
+ gc.getMonth()
+ gc.getDay()
+ gc.getHour()
+ gc.getMinute()
+ gc.getSecond(); //Where's milliseconds???
}






java hashcode milliseconds xmlgregoriancalendar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:57









Vadim

3,3932622




3,3932622










asked Nov 21 '18 at 15:57









Joey CorkeyJoey Corkey

809




809













  • Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

    – Vadim
    Nov 21 '18 at 16:24











  • @Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

    – Joey Corkey
    Nov 21 '18 at 16:53






  • 2





    1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

    – Vadim
    Nov 21 '18 at 17:59






  • 1





    In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

    – Ole V.V.
    Nov 26 '18 at 17:17






  • 1





    Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

    – Ole V.V.
    Nov 27 '18 at 11:13



















  • Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

    – Vadim
    Nov 21 '18 at 16:24











  • @Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

    – Joey Corkey
    Nov 21 '18 at 16:53






  • 2





    1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

    – Vadim
    Nov 21 '18 at 17:59






  • 1





    In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

    – Ole V.V.
    Nov 26 '18 at 17:17






  • 1





    Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

    – Ole V.V.
    Nov 27 '18 at 11:13

















Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

– Vadim
Nov 21 '18 at 16:24





Please read documentation about what Hash Code is in Java - clue: there is no requirements for hasCode to be unique - there is only one "same input must produce same resulting hashCode". So do not use hashCode method to distinguish objects or values.

– Vadim
Nov 21 '18 at 16:24













@Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

– Joey Corkey
Nov 21 '18 at 16:53





@Vadim I now see why one should not use it to distinguish objects or values. What would hashCode() be useful for then?

– Joey Corkey
Nov 21 '18 at 16:53




2




2





1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

– Vadim
Nov 21 '18 at 17:59





1. It is heavily used in hash based collection implementations such as HashSet, HashMap etc. mostly for performance. 2. it is good to use in equals() method (or for for property based object comaprisions)- when a.hashCode() != b.hasCode() nothing else need to be checked, but only when hashCode() is implemented. Generally speaking every DTO/POJO classes must implement it. But unfortunately low qualified developers don't do that very often...

– Vadim
Nov 21 '18 at 17:59




1




1





In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

– Ole V.V.
Nov 26 '18 at 17:17





In the source code I found a comment saying that the hash code (for the class concretely implementing the XMLGregorianCalendar abstract class) should be consistent with equals. However, equals does distinguish different milliseconds values, hashCode does not. While this is not strictly against the specs, it does look like a bug in my eyes.

– Ole V.V.
Nov 26 '18 at 17:17




1




1





Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

– Ole V.V.
Nov 27 '18 at 11:13





Possible duplicate of Are two Java objects with same hashcodes not necessarily equal?

– Ole V.V.
Nov 27 '18 at 11:13












0






active

oldest

votes











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%2f53415917%2fxmlgregoriancalendar-hashcode-does-not-handle-milliseconds%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53415917%2fxmlgregoriancalendar-hashcode-does-not-handle-milliseconds%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

Costa Masnaga

Fotorealismo

Sidney Franklin