0

i think this question might be duplicate,but from so many time i am searching the solution of this issue and not getting satified result.my problem is i want to find a time interval between two dates in secound.for that i am having time stamp value,i am able to convert time stamp to date like below

Date newDate = new Date();

newDate.setTime(Long.parseLong("-978307200")*1000L);

and i am getting such date after converting timestamp to date

Sun Jan 01 05:30:00 GMT+05:30 1939

my problem is i want to find time interval in secound between this date and year 1970,so any date which is less than 1970 for that i want to count time interval in secound.

it will be my pleasure if any one can help to solve this issue

thanks in advance Aamirkhan I.

Aamirkhan
  • 5,746
  • 10
  • 47
  • 74
  • 2
    What do you mean? You already seem to have the value you're asking for: 978307200 seconds is the difference between 01/01/1939, 05:30:00 in timezone GMT+05:30 and 01/01/1970, 00:00:00 GMT. – Jesper Nov 21 '12 at 14:29
  • how did you calculated this different can you tell me?sorry i am very bad with date time in java,can you show me code snipped? – Aamirkhan Nov 21 '12 at 14:33
  • The number is in your own code! `newDate.setTime(Long.parseLong("-978307200")*1000L);` – Jesper Nov 21 '12 at 14:33
  • no i think you did not get my query properly ,`newDate.setTime(Long.parseLong("-978307200")*1000L);` this one is parsing my time stamp to date,now i want to compare this date with year `1970` and want to calculate time interval in secound between this date and year 1970,i hope you got my query properly now – Aamirkhan Nov 21 '12 at 14:35
  • 978307200 it is not secound it is time stamp value – Aamirkhan Nov 21 '12 at 14:37
  • It's the number of seconds between the two dates. That's what you're asking for, isn't it? A `java.util.Date` contains the difference between the date it's set to and 01/01/1970, 00:00:00 GMT in milliseconds. – Jesper Nov 21 '12 at 15:40

2 Answers2

0

If you are having trouble with the standard Date api - try Joda Time - it's much more convenient.

Joda Time

Also see this stack overflow post on a similar matter:-

How to calculate elapsed time from now with Joda Time?

Community
  • 1
  • 1
Davos555
  • 1,974
  • 15
  • 23
-1

It is better to use Calendar API instead Date. Calendar API is much more useful.

Anyway to get number of Seconds from Date object you can getTime method.

Date date = new Date();
(date.getTime()/1000)

getTime

public long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. Returns: the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

Ref: http://docs.oracle.com/javase/6/docs/api/

Vishal
  • 3,189
  • 1
  • 15
  • 18