Defensive copy of Calendar

前端 未结 7 1455
面向向阳花
面向向阳花 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:58

    What about the below ?

    public synchronized void setDate(Calendar date) {
        // What to do.... 
        Calendar anotherCalendar = Calendar.getInstance();
        anotherCalendar.setTimeInMillis(date.getTimeInMillis());
    }
    

    The correct usage in code of synchronized depends on your use case.

提交回复
热议问题