Results 1 to 4 of 4
  1. #1
    Pazuzu156 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Need help with Date

    I'm building a stopwatch, and everything is doing just fine except 1 thing, the Date.

    I've tried Calendar, Date, DateTime, all of it.

    Here is the issue:
    To get the time that's passed, I use now - startTime

    Now is initialized every time the while loop runs, startTime was initialized when the "start" button was pressed. The time keeps track correctly however instead of starting at 00:00:00
    like it should, it starts with 07:00:00

    As in, Date starts it 7 hours ahead! What do I do to fix this?

    Here's a sample of what the code I'm using looks like

    Java Code:
    long now = System.currentTimeMillis();
    long diff = (now - startTime);
    
    DateFormat swFormatter = new SimpleDateFormat("hh:mm:ss");
    
    Date res = new Date(diff);
    					
    lblCounter.setText(swFormatter.format(res));
    I'll say, not being able to figure this out is getting me a tad on edge. I'd really love some help.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Need help with Date

    Have you check what is the value of diff when you creating a new instance of Date in line 6?

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,877
    Rep Power
    5

    Default Re: Need help with Date

    I don't get that code anyway; you calculate the difference between two dates and then you pass that to the Date constructor; that is going to create quite warped dates (try also printing the year for example).

    Check the javadoc of the Date constructor, you'll see that your assumptions are wrong.
    Last edited by gimbal2; 01-24-2014 at 09:59 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Pazuzu156 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: Need help with Date

    Yeah.. I have no idea what I did wrong, probably don't wanna try to fix it. I just wound up writing a math function and displaying my result with String.format().

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  2. Replies: 4
    Last Post: 07-27-2011, 11:57 AM
  3. Replies: 2
    Last Post: 05-12-2011, 04:52 PM
  4. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  5. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 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
  •