Findbugs issues with mutability of Date object in Java
JodaTime has immutable dates.
Sure, it's okay to use a Date
constructor in a getter, why wouldn't it be?
That said, just because FindBugs pegs mutable state as a potential error, it doesn't mean it's intrinsically worth caring about–it depends on how the class is being used. Immutability eliminates one type of bug, which you may or may not need to care a lot about.
Attention Folks...
besides adapting both the getter and the setter you need to take care about null values:
public Date getSomeDate() {
if (this.someDate == null) {
return null;
}
return new Date(this.someDate.getTime());
}
public void setSomeDate(final Date someDate) {
if (someDate == null) {
this.someDate = null;
} else{
this.someDate = new Date(someDate.getTime());
}
}