It's not a secret anymore!*
The moose likes Beginning Java and the fly likes getting the time in milliseconds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting the time in milliseconds" Watch "getting the time in milliseconds" New topic
Author

getting the time in milliseconds

sean beacham
Greenhorn

Joined: Apr 12, 2011
Posts: 20
why is it when i set this:



The date starts from 1970???

my last reading in milli seconds was this 1302647947945 which turns out to be 41 years!!

i just want the specific time of the day in milliseconds
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sean beacham
Greenhorn

Joined: Apr 12, 2011
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Oct 13, 2005
Posts: 37891
    
  22
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
sean beacham
Greenhorn

Joined: Apr 12, 2011
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Apr 12, 2011
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.



[Thumbnail for Untitled.png]

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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

Joined: Apr 12, 2011
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

Joined: Oct 13, 2005
Posts: 37891
    
  22
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

Joined: Apr 12, 2011
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

Joined: Apr 12, 2011
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

Joined: Oct 13, 2005
Posts: 37891
    
  22
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

Joined: Apr 12, 2011
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

Joined: Oct 13, 2005
Posts: 37891
    
  22
Can't you use the System methods which give the time, from the system clock, and the Calendar method which takes time in milliseconds?
 
wood burning stoves
 
subject: getting the time in milliseconds
 
Similar Threads
Convert hh:mm:ss in millisecond
Time related issues
Date calculation
milliseconds
June Newsletter Puzzle