Defensive copy of Calendar

前端 未结 7 1392
面向向阳花
面向向阳花 2021-01-04 00:38

Been trying to find the best way to implement a method that makes a defensive copy of a Calendar object.

eg:

public void setDate(Calendar date) {
            


        
7条回答
  •  离开以前
    2021-01-04 00:52

    Simplest way would be:

    copy = Calendar.getInstance(original.getTimeZone());
    copy.setTime(original.getTime());
    

    But I strongly suggest that (whenever possible) you use JodaTime to express times and dates in Java. It has immutable classes as well as mutable.

提交回复
热议问题