Why are different long values converted into the same date/time?

放肆的年华 提交于 2019-12-10 21:12:40

问题


public static void getTime() {
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
    Time t1 = new Time(Long.parseLong("1369213412435"));
    Time t2 = new Time(Long.parseLong("1369213412245"));
    System.out.println(sdf.format(t1));
    System.out.println(sdf.format(t2));
}

Why does the above code prints,

2013-05-22 17:03:32
2013-05-22 17:03:32

回答1:


The two dates differ only by milliseconds (435 or 245), which you ignore in your format.

Use:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

to see the different values.




回答2:


The only difference is in the milliseconds (435 vs 245).




回答3:


The two dates are differed by milliseconds i.e. 435 and 245.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

This will do.




回答4:


Use :

 SimpleDateFormat sdf = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss:SSS"); 

and you will see the difference in the milliseconds part.




回答5:


Apart from the millisecond parts there are certain cases where two long values can provide same date.

i.e. below two long values have same millisecond and are different

  • 1458065184000
  • 1458021984000

If you use dd-MM-yyyy hh:mm:ss SSS then it will give you the same result.

Catch here is hh (12 hr format) vs HH (24 hr format).

Using this will give the accurate result dd-MM-yyyy HH:mm:ss SSS



来源:https://stackoverflow.com/questions/16708785/why-are-different-long-values-converted-into-the-same-date-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!