programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# getting the time in milliseconds

Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
why is it when i set this:

The date starts from 1970???

i just want the specific time of the day in milliseconds

Sheriff
Posts: 67747
173
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
what kind of equation surely there must be a statement for get current time in milliseconds for the day? :\
i know :\
1 hour = 3600000

Marshal
Posts: 28236
95
• Number of slices to send:
Optional 'thank-you' note:

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?

Marshal
Posts: 79263
377
• Number of slices to send:
Optional 'thank-you' note:
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.

Java Cowboy
Posts: 16084
88
• 3
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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.... https://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
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 16084
88
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 79263
377
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 79263
377
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
Marshal
Posts: 79263
377
• Number of slices to send:
Optional 'thank-you' note:
Can't you use the System methods which give the time, from the system clock, and the Calendar method which takes time in milliseconds?

 Why is the word "abbreviation" so long? And this ad is so short? a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com