Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Difference in time with alternative methods

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difference in time with alternative methods

    Hi all,

    I'm not looking for the answer but rather a point in the right direction would be most welcome.

    I have used two methods to tell the current time from System.currentTimeMillis()

    The first method using the java.util.Date gives me the correct time, but the second method (using long calculation) gives me the time - 1 hour. e.g: util.Date giving 9:30am and the long method giving 8:30am

    I can't figure out why the two times would be different.

    Any and all advice welcome.

    Code below:

    import java.util.Date;
    class Q8
    {
    public static void main (String[]args)
    {
    long currentMillisecond = System.currentTimeMillis();
    Date currentDate = new Date(currentMillisecond);
    System.out.println("The current date is: " + currentDate);

    long seconds = currentMillisecond/1000;
    long currentSecond = seconds%60;

    long minutes = seconds/60;
    long currentMinute = minutes%60;

    long hours = minutes/60;
    long currentHour = hours%24;

    System.out.println("The current time is: " + currentHour + ":" + currentMinute + ":" + currentSecond);
    }
    }


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difference in time with alternative methods

    First of all System.currentTimeMillis() is based on your chip time, which is NOT the time your clock is.
    Second of all I can pin point it but something in the calculations seems wrong to me.
    Third of all NEVER EVER user numbers in your class names. I know it is not your question, but it is bad conduct.
    Fourth of all I have run this on 3 different chipsets now and I get 3 different outputs, so I suspects it has something to do with number one,
    read about the currentTimeMillis here: System (Java Platform SE 7 )
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Difference in time with alternative methods

    As tools has pointed out, currentTimeMillis() does not take into account your local clock at all, only the unix epoch time since January 1970. This fact is also true for the Date object internal timing. However, the toString() method for Date does use your local time, but no other part of it does. This is typically a great source for confusion. If you want your local date in milliseconds, I suggest you use:

    Calendar.getInstance(TimeZone.getDefault()).getTimeInMillis();
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference in time with alternative methods

    Thanks all.

    1: The only reason i set a class with a number is that is what is being asked of me by my lecturer. e.g: questions 1-9, name the programs and classes within as q1 q2 q3 etc.
    2: Why is this bad practice? Far as i can tell, there is no universally agreed naming convention, so i would leave good/bad practice to the decision of the employer surely?
    3: is there any way to force java to take daylight savings into account when using the long calculation method?

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Difference in time with alternative methods

    1. Fair and square
    2. I have always been told it both by Lectures, my employers and the dude from Oracle who taugth my class once
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Difference in time with alternative methods

    Quote Originally Posted by CruelCoin View Post
    Far as i can tell, there is no universally agreed naming convention
    Yes there is. See Code Conventions for the Java Programming Language

    Quote Originally Posted by CruelCoin View Post
    3: is there any way to force java to take daylight savings into account when using the long calculation method?
    See TimeZone (Java Platform SE 7 )

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difference in time with alternative methods


    Thanks! i have posted that convention link to my student group.

    Cheers,

    CC.

Similar Threads

  1. difference between preemptive scheduling and time slicing
    By vasanthjayaraman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2013, 05:46 PM
  2. alternative to JMF
    By olimpicco in forum Java Theory & Questions
    Replies: 0
    Last Post: December 19th, 2011, 09:50 PM
  3. 1st time using methods
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 1st, 2010, 02:37 PM
  4. JNI Alternative?
    By janusmccarthy in forum Java Native Interface
    Replies: 1
    Last Post: November 14th, 2009, 12:22 PM