new SimpleDateFormat always returns same reference for a given dateFormat

前端 未结 3 624
暗喜
暗喜 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:40

    SimpleDateFormat actually implements hashCode by returning the hashcode of the pattern.

    You can verify that there are actually distinct objects by using System.identityHashCode():

    System.out.println("d1 = " + d1 + " / " + System.identityHashCode(d1));
    System.out.println("d2 = " + d2 + " / " + System.identityHashCode(d2));
    System.out.println("d3 = " + d3 + " / " + System.identityHashCode(d3));
    

    This will print 3 different values.

提交回复
热议问题