Johnny Chen
Johnny Chen

Reputation: 2070

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

Upvotes: 3

Views: 329

Answers (5)

user2672763
user2672763

Reputation: 811

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

Upvotes: 0

Shreyos Adikari
Shreyos Adikari

Reputation: 12754

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.

Upvotes: 4

giorashc
giorashc

Reputation: 13713

Use :

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

and you will see the difference in the milliseconds part.

Upvotes: 3

Andrea Bergia
Andrea Bergia

Reputation: 5552

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

Upvotes: 4

BobTheBuilder
BobTheBuilder

Reputation: 19304

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.

Upvotes: 10

Related Questions