JAVA system clock help

Hey. I'm writing a program in JAVA that will take the time in milliseconds from the computer and put it into the hour:minute:seconds:milliseconds format. Everyithng works fine except that the hour number is off by a few thousand. Hear's my code:
[PHP]//Fabs
//1.9.07
public class clock
{ public static void main(String str[])
{ long mil = System.currentTimeMillis();
long hr =mil / 3600000;
mil = mil % 3600000;
long min = mil / 60000;
mil = mil % 60000;
long sec = mil / 1000;
mil = mil % 1000;
if(min < 10)
{ System.out.println(hr + ":0" + min + ":" + sec + ":" + mil);
if(sec < 10)
{ System.out.println(hr + ":0" + min + ":0" + sec + ":" + mil);
}
}
else
{ if(sec < 10)
{ System.out.println(hr + ":" + min + ":0" + sec + ":" + mil);
}
else
{ System.out.println(hr + ":" + min + ":" + sec + ":" + mil);
}
}
}
}[/PHP]

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    check the docs for exactly what System.currentTimeMillis() does. It's not doing what you think it is.

    edit:

    your if statements are a little wonky, as well.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2007
    Well, my interest was piqued, so I did a little looking.
    urrentTimeMillis

    public static long currentTimeMillis()

    Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

    See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC).

    Returns:
    the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
    See Also:
    Date

    so that means you are going to need to get rid of all the extra crap first. I figured there should be 3.1556926E10 ms in a year. and from there. its fairly accurate and accounts for leap year. not sure if I should have or not. but considering one revolution is technically a year, which takes 365.25 days. anyways..

    you should be on your way now. I was going to re-write it for you, but I'm busy with other things.
  • edited January 2007
    okay. i'm trying to find the date, so i used [PHP]import java.util.Date;[/PHP] to import the date class and then used this line of code[PHP]int year = getYear();[/PHP] . This gave me an error when i compiled it which said [PHP]C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:27: cannot resolve symbol
    symbol : method getYear ()
    location: class clock
    int g = getYear();
    ^
    1 error

    Tool completed with exit code 1[/PHP]
    Do you know what is wrong? because my java sdk is 1.4.2 and the method getYear() is under the date class for the api for 1.4.2
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    okay. i'm trying to find the date, so i used [PHP]import java.util.Date;[/PHP] to import the date class and then used this line of code[PHP]int year = getYear();[/PHP] . This gave me an error when i compiled it which said [PHP]C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:27: cannot resolve symbol
    symbol : method getYear ()
    location: class clock
    int g = getYear();
    ^
    1 error

    Tool completed with exit code 1[/PHP]
    Do you know what is wrong? because my java sdk is 1.4.2 and the method getYear() is under the date class for the api for 1.4.2

    when you call getYear() on its own, the java compiler looks for a method getYear() (that you wrote!) in the current file.

    If you want the current year, you'd probably want:
    Date d = new Date();
    int year = d.getYear();
    

    Also, I think Date is deprecated (it works, but may be removed). Calendar is a non-deprecated option:
    Calendar c = new Calendar();
    int year = c.getYear();
    
  • edited January 2007
    okay. I did
    Calendar d = new Calendar();
    		int year = Calendar.get(Calendar.YEAR);
    
    and it gave me this error:
    C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:27: java.util.Calendar is abstract; cannot be instantiated
    		Calendar d = new Calendar();
                                 ^
    C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:28: non-static method get(int) cannot be referenced from a static context
    		int year = Calendar.get(Calendar.YEAR);
                                       ^
    2 errors
    
  • airbornflghtairbornflght Houston, TX Icrontian
    edited January 2007
    is Calendar a static class?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    okay. I did
    Calendar d = new Calendar();
    		int year = Calendar.get(Calendar.YEAR);
    
    and it gave me this error:
    C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:27: java.util.Calendar is abstract; cannot be instantiated
    		Calendar d = new Calendar();
                                 ^
    C:\Documents and Settings\Fabiano\Desktop\Java stuff\clock.java:28: non-static method get(int) cannot be referenced from a static context
    		int year = Calendar.get(Calendar.YEAR);
                                       ^
    2 errors
    

    Sorry, my code was wrong:
    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    

    I just realized that I usually use GregorianCalendar, which isn't an abstract class (can't be instantiated), which is why the first code I gave you doesn't work. I checked, and they're actually the same thing:

    Calendar c = Calendar.getInstance();
    GregorianCalendar g = new GregorianCalendar();
    System.out.println(c.equals(g)); //This outputs TRUE.
    

    I guess it's possible that if you're somewhere that doesn't use the gregorian calendar, there may be a difference.
  • edited January 2007
    I fixed it. [PHP]import java.util.Calendar;
    import java.util.Date;
    public class clock
    { public static void main(String str[])
    { Calendar c = Calendar.getInstance();
    Date q = c.getTime();
    System.out.println(q);
    }
    }[/PHP]
    Thanx for the help. Also, do you know to use the data given to split it up into three differnet ints(one containing the hour, one containing the minutes, and one containig the seconds)?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2007
    rather than putting the time into a date, you can just use:

    [php]
    Calendar c = Calendar.getInstance();
    int h = c.get(Calendar.HOUR);
    int s = c.get(Calendar.SECOND);
    int m = c.get(Calendar.MINUTE);
    int amPm = c.get(Calendar.AM_PM);
    [/php]
  • edited January 2007
    Calendar is abstract, but the reason why it can't be instantiated is that it is a singleton. singletons have a private constructor and have a defacto 'getInstace()' method that returns an instance of the class. this gives the singleton class full control over exactly *how* it is created. as by design, the JVM can only ever produce ONE instance of a singleton class during its lifetime.
Sign In or Register to comment.