new SimpleDateFormat always returns same reference for a given dateFormat

前端 未结 3 656
暗喜
暗喜 2021-01-17 22:41


I was trying to replicate a bug by using the same instance of SimpleDateFormat across multiple threads. However I got stuck with another problem and did not find any

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 23:43

    They are different instances, try this

        DateFormat d1 = new SimpleDateFormat("ddMMyyyy");
        DateFormat d2 = new SimpleDateFormat("ddMMyyyy");
        System.out.println(d1 == d2);
    

    it prints

    false
    

    as for the same java.text.SimpleDateFormat@c5bfbc60, they are based on class name and hashCode. According to Object.hashCode API it does not necessarily return distinct values for distinct objects

提交回复
热议问题