Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# getting the time in milliseconds

sean beacham
Greenhorn
Posts: 20
why is it when i set this:

The date starts from 1970???

i just want the specific time of the day in milliseconds

Bear Bibeault
Author and ninkuma
Marshal
Posts: 64629
86
Time is stored as number of milliseconds since Jan 1, 1970. If you want the milliseconds within the current day, you'll need to do some math.

sean beacham
Greenhorn
Posts: 20
what kind of equation surely there must be a statement for get current time in milliseconds for the day? :\
i know :\
1 hour = 3600000

Paul Clapham
Sheriff
Posts: 20757
30
sean beacham wrote:surely there must be a statement for get current time in milliseconds for the day? :\

Well, actually, no, there isn't. You say that as if it's a common thing to need the number of milliseconds since midnight, but I don't think it is.

So before we get into the calculations for finding that number (which are more complicated than you might think), can you tell us why you think you need that number?

Campbell Ritchie
Sheriff
Posts: 48404
56
If you need the milliseconds since midnight, you can get the hour, minute and second and multiply them.
Note the millisecond method is not precise; some computers used to work to the nearest 0.1s, so milliseconds were only precise to the nearest 100.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
• 3
Another way:

Get a Calendar object, set it to today, and set the hours, minutes, seconds and milliseconds to 0. Call getTimeInMillis() on it.

Call System.currentTimeMillis() to get the current time in milliseconds. Subtract the value that you got above from it, and you have the number of milliseconds since midnight.

sean beacham
Greenhorn
Posts: 20
Jesper de Jong wrote:Another way:

Get a Calendar object, set it to today, and set the hours, minutes, seconds and milliseconds to 0. Call getTimeInMillis() on it.

Call System.currentTimeMillis() to get the current time in milliseconds. Subtract the value that you got above from it, and you have the number of milliseconds since midnight.

will that refresh for every day?

what im trying to do is this.... http://www.coderanch.com/t/534155/GUI/java/Swing-Calender-ComboBox-timer-questions

i have explained it there im at witts end with it right now :\

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
sean beacham wrote:will that refresh for every day?

I'm not sure what you mean by that. When you call System.currentTimeMillis(), you get the time at the moment you're calling it.

sean beacham
Greenhorn
Posts: 20
ok what i mean is

the user has set the timer for continous use until the timer is reprogrammed therefore i want it to check the time constantly and when the time corresponding time arrives Turn On & Off the boiler sets the variable to switch off? do you understand what i mean? So obviously it needs to know when to turn on and off so its comparing the time the user has set to the time on the pc.

Untitled.png

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
• 1
Have a look at class java.util.Timer, which will let you schedule a task to be executed at a particular date and time.

sean beacham
Greenhorn
Posts: 20
i changed my mind about getting time in milliseconds im now using gregorian calender which gets the current hour and minute

but right now im getting a silly error, all elements in this statement are ints now i dont get why its being a pain!

The operator && is undefined for the argument type(s) int, boolean

for this specific line:

if (hourOn && minOn == timerhour24 && timermin)

the full code is here:

Campbell Ritchie
Sheriff
Posts: 48404
56
• 1
You can't use the && operator like that. I am not quite sure what that means, but do you mean you want a particular turn-on time? You would probably want if (min == minOn && hour == HourOn) . . .
By the way: there is a better way to fill your arrays, which I edited to shorten the lines.I am not sure why you need those String arrays at all. You can print out the numbers with the %d tag and not use Strings.

sean beacham
Greenhorn
Posts: 20
Campbell Ritchie wrote:You can't use the && operator like that. I am not quite sure what that means, but do you mean you want a particular turn-on time? You would probably want if (min == minOn && hour == HourOn) . . .
By the way: there is a better way to fill your arrays, which I edited to shorten the lines.I am not sure why you need those String arrays at all. You can print out the numbers with the %d tag and not use Strings.

yes i want to switch something on at a particular time ive modified my code and thats working thankyou although values of 00-09 only produce 1 digit so 00=0 and 09=9 its strange!

1 other thing my statement does nothing!

sean beacham
Greenhorn
Posts: 20
i manged to fix the statement problem i was showing my instance variables so i had to remove the int on this statement

still havent fixed the 00 to 09 problem though :\

Campbell Ritchie
Sheriff
Posts: 48404
56
sean beacham wrote:. . . thats working thankyou although values of 00-09 only produce 1 digit so 00=0 and 09=9 its strange! . . .
You're welcome

I am surprised you are getting 0 rather than 00. Try this, and I bet you will get 00.Which statement does nothing?
Why are you using null layour and setBounds?

sean beacham
Greenhorn
Posts: 20
the statement works perfectly fine now thankyou i fixed that yesterday.

im now using swing timers to loop the statement to see if it will change however the gregorian calender only seems to run once that is when you first open the program. Ive tried putting a timer around it and an infinate for loop to see if that works but it doesnt its quite annoying! i kept getting an error. Therefore timerhour24 timermin is a static time that at the time you open the program! and i want to update it in real time.

Campbell Ritchie
Sheriff
Posts: 48404
56
Can't you use the System methods which give the time, from the system clock, and the Calendar method which takes time in milliseconds?