Results 1 to 6 of 6

Thread: Java date function

  1. #1
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts

    Java date function

    How do you call this function. I am trying to calculate the time taken to perform a calculation, here is my cut down pseudo.

    Code:
    date_before = current_date;
    
    PERFORM CALCULATION
    
    time_taken = current_date - date_before;
    HEEEEEEEEEEEELP?!

    I know theres a built in date function but i dont know how to call it.

  2. #2
    Pixel Abuser Spunkey's Avatar
    Join Date
    Nov 2003
    Location
    Milton Keynes
    Posts
    1,523
    Thanks
    0
    Thanked
    0 times in 0 posts
    hi matey!

    I've just had to do something similar myself, and being a VB/ASP programmer by trade i though JS would have some kind of datediff function, but it doesnt!! (AFAIK, could be wrong)

    however if you can do this....

    Code:
     dBeforeDate = Date.parse(todays_date)
    
     .. Do Some Stuff
    
     dAfterDate = Date.parse(todays_date)
    
      iTimeTaken = dAfterDate - dBeforeDate // in milliseconds
    the date.parse will return an integer of the number of milliseconds elapsed since the given date and the 1st of January 1970 - i kid ye not.

    anyhoo, you can work out the seconds/minutes from the returned iTimeTaken.

    HTH!
    Last edited by Spunkey; 28-04-2004 at 05:23 PM.

  3. #3
    Commander Keen
    Join Date
    Nov 2003
    Location
    217.27.240.214
    Posts
    624
    Thanks
    0
    Thanked
    0 times in 0 posts
    That is a good suggestion. However, I think you might need a pointer to the API for this one.

    http://java.sun.com/j2se/1.4.2/docs/api/index.html

    Look at that for the roots of the rest of this reply. The java API is your friend.

    the algorithm should be something like...

    long timeOneInMilliSeconds = new Date().getTime() ; // this makes a new Date object and then sets the long integer value of the Date object (milliseconds since january 1st 1970 - a unix legacy idea) into the timeOneInMilliSeconds variable..

    // Do lots of funky and useful things

    long timeTwoInMilliSeconds = new Date().getTime() ; // this makes a new Date object and then sets the long integer value of the Date object into the timeTwoInMilliSeconds variable..

    long difference = timeTwoInMilliSeconds - timeOneInMilliSeconds ;
    ..........

    Then to get the difference in a useful form.. Like a Date obeject. Do something like the following.

    Date useful = new Date(difference) ; // This is a constructor for the Date object where the parameter is a long integer.
    -------------------------------------------------------------------------------
    hope that helps bud

  4. #4
    dgr
    dgr is offline
    Senior Member
    Join Date
    Jul 2003
    Posts
    621
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by Nemeliza
    How do you call this function. I am trying to calculate the time taken to perform a calculation, here is my cut down pseudo.

    Code:
    date_before = current_date;
    
    PERFORM CALCULATION
    
    time_taken = current_date - date_before;
    HEEEEEEEEEEEELP?!

    I know theres a built in date function but i dont know how to call it.
    word of warning, java developers prefer if your variables are named dateBefore, currentDate, timeTaken, etc. Your classes should be called FruitClass, VehiclesAndAnimals, etc.
    dothan 745 @ 2.4ghz | 2gb Corsair XMS (2-3-3-6) | dual raptors (raid0) | ATI 9700pro | CM201 | dual lg 1810

  5. #5
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    I find the System.currentTimeMillis() useful for timing things.

    e.g.

    long startTime = System.currentTimeMillis();
    doStuff();
    long endTime = System.currentTimeMillis();
    System.out.println("Time taken = "+(endTime-startTime)+"ms");

    cheers,
    dave

  6. #6
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts
    Thanks got it going.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Java Problems
    By Applecrusher in forum Software
    Replies: 4
    Last Post: 27-04-2004, 01:12 PM
  2. Computer Architecture Question : Function & Address Decoder?
    By Steven_McL in forum General Discussion
    Replies: 3
    Last Post: 29-03-2004, 11:39 PM
  3. Java
    By Jonny M in forum Software
    Replies: 8
    Last Post: 24-12-2003, 02:33 PM
  4. Running rmid from a java prog
    By killgORE in forum Software
    Replies: 1
    Last Post: 02-12-2003, 06:46 PM
  5. Java vs .NET
    By DaBeeeenster in forum Software
    Replies: 2
    Last Post: 11-09-2003, 02:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •