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]
[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]
0
Comments
edit:
your if statements are a little wonky, as well.
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.
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:
Also, I think Date is deprecated (it works, but may be removed). Calendar is a non-deprecated option:
Sorry, my code was wrong:
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:
I guess it's possible that if you're somewhere that doesn't use the gregorian calendar, there may be a difference.
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)?
[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]