简体   繁体   中英

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

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

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.

唯一的区别在于毫秒(435 vs 245)。

The two dates are differed by milliseconds ie 435 and 245 .

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

This will do.

Use :

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

and you will see the difference in the milliseconds part.

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

ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM