Stream Security Calendar.getInstance

If I have the following method; Is the call Calendar.getInstance()thread safe?

 public Date currentTime() {
    Date d = null;
    synchronized(this){
        d = Calendar.getInstance().getTime();
    }
    return d;
 }

My understanding is Calendar.getInstance()not thread safe, so the following method cannot be thread safe. Is it correct?

public Date currentTime() {
    return Calendar.getInstance().getTime();

}

I understand that Joda-Time is touted as best practice for date-based APIs, but here I am stuck with the standard java.util.Date/ Calendar classes.

+4
source share
2 answers

, Calendar.getInstance() , . , , getTime() .

+8

? - , . , . .

+1

Source: https://habr.com/ru/post/1527255/


All Articles