Best way to clone java.time.LocalDateTime

  • A+
Category:Languages

I would like to create a copy java.time.LocalDateTime but it does not have clone() method.

What I do is the following:

long epochMilli = Instant.now().toEpochMilli();  LocalDateTime createDate =  LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());  LocalDateTime modificationDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault()); 

Is there an easyest way to create two LocalDateTime objects with the exact same date-time value?

 


Because LocalDateTime is immutable, you can simply reference the same object:

LocalDateTime createDate = LocalDateTime.now();  LocalDateTime modificationDate = createDate; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: