Reputation: 4115
I tried to compare date and time of two timeStamp long values as follows.
Timestamp st1 = new Timestamp(1313045029);
Timestamp st = new Timestamp(1313045568);
System.out.println("Date:"+st.getDate());
System.out.println("Day:"+st.getDay());
System.out.println("Year:"+(st.getYear()+1900));
System.out.println("Month:"+(st.getMonth()+1));
System.out.println("Hours:"+st.getHours());
System.out.println("Minutes:"+st.getMinutes());
System.out.println("Seconds:"+st.getSeconds());
System.out.println("*********************************");
System.out.println("Date1:"+st1.getDate());
System.out.println("Day1:"+st1.getDay());
System.out.println("Year1:"+(st1.getYear()+1900));
System.out.println("Month1:"+(st1.getMonth()+1));
System.out.println("Hours1:"+st1.getHours());
System.out.println("Minutes1:"+st1.getMinutes());
System.out.println("Seconds1:"+st1.getSeconds());
There should be some difference in time, but in the output there is difference at all? Please help.
Upvotes: 2
Views: 9212
Reputation: 7612
There is difference. But difference is not in Years, Months, Days, Hours, Minutes or Seconds.
The difference is very minute ie in milliseconds.
Timestamp constructor takes values in milliseconds and convert it into timestamp.
1313045029 - 1313045568 = 539
The difference is 539ms ie 0.5secs
Upvotes: 2
Reputation: 691635
The value represents milliseconds. And you only have 529 milliseconds of difference between the two timestamps. So, there is a good chance that they are indeed in the same second.
Upvotes: 2
Reputation: 1926
You would need to use a getNanos() call to get the rest of the difference
Upvotes: 1
Reputation: 2999
You can convert milliseconds to date with this code:
long now = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
Upvotes: 0