This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/2/14

Timer

1
2
Nosson1459 Nosson1459

2017/2/14

#
I'm making a timer program and I'm wondering if I am supposed to use System.currentTimeMillis() for the elapsed time or somehow use java.util.Calendar to calculate the milliseconds passed, which I don't know how to if I am supposed to? By supposed to I mean it will be simpler to use if I knew how, and I can then use it in other places once I do know how.
danpost danpost

2017/2/14

#
Nosson1459 wrote...
I'm making a timer program and I'm wondering if I am supposed to use System.currentTimeMillis() for the elapsed time or somehow use java.util.Calendar to calculate the milliseconds passed, which I don't know how to if I am supposed to? By supposed to I mean it will be simpler to use if I knew how, and I can then use it in other places once I do know how.
I think it would be more in the lines of preference. Of course, that preference would probably be reflected in what type of field you used to store the time(s). If you have specific questions on the use of the Calendar object, ask away.
Nosson1459 Nosson1459

2017/2/15

#
While I was waiting for an answer (I only just saw it now even though it says "22 hours ago" by both of us) I used System.currentTimeMillis() and it works fine technically. I was wondering how I would use the Calendar class for this because when I was looking at the API the things I saw there didn't really have much to do with getting the current time that's why I had to ask about that, so I figured all that stuff has to be for something but I'm not sure how to use any of it and/or where.
danpost danpost

2017/2/15

#
Did you ever play around with or look into the codes given here? Print out the results to the terminal to see what values are given the new instance when line 1 is executed.
Nosson1459 Nosson1459

2017/2/16

#
danpost wrote...
Did you ever play around with or look into the codes given here? Print out the results to the terminal to see what values are given the new instance when line 1 is executed.
What do you mean? You showed me that code for retrieving the current day of the week, month, date, and year, so used it for that, what else is there in that code? When you say line 1, you mean "Calendar calendar = Calendar.getInstance();"? I thought you need that for getting all the current times so I used it (right before I display the current time) and it works.
danpost danpost

2017/2/16

#
Nosson1459 wrote...
You showed me that code for retrieving the current day of the week, month, date, and year, so used it for that, what else is there in that code?
Yes, that is all you ask about in your initial post. But, did you ever look at the API documentation of the Calendar class?
When you say line 1, you mean "Calendar calendar = Calendar.getInstance();"? I thought you need that for getting all the current times so I used it (right before I display the current time) and it works.
That is not what it is used for. When creating a Calendar instance, you are getting an object with field values that represent the current date and time. There are also the following fields that can be retrieved from the object: Calendar.HOUR Calendar.MINUTE Calendar.SECOND Calendar.MILLISECOND There values are also set to the moment the Calendar instance was created.
Nosson1459 Nosson1459

2017/2/16

#
I forgot we didn't mention time. I used the first three of those above in the same place that I used the dates, so since I already knew about them I didn't know what else you would be referring to without looking at the API.
danpost danpost

2017/2/16

#
Nosson1459 wrote...
I forgot we didn't mention time. I used the first three of those above in the same place that I used the dates, so since I already knew about them I didn't know what else you would be referring to without looking at the API.
There is also the 'getTIme' method which returns a Date object of the same instance in time. As a Date object, you can convert to a formatted String object. For example, execute the following:
1
2
3
Calendar calendar = Calendar.getInstance();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
System.out.println(df.format(calendar.getTime()));
Of course, you will need the following imports:
1
2
3
import java.util.Calendar;
import java.text.DateFormat;
import java.util.Locale;
Nosson1459 Nosson1459

2017/2/16

#
  • Why is the Locale part needed, I get the same results with
    1
    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(Calendar.getInstance().getTime()));
    ?
  • System.currentTimeMillis() is the same as Calendar.getTimeInMillis()?
  • The Calendar.getTime() is the same as
    1
    2
    3
    Date today = new Date();
    // and I can then do:
    System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(today)); // or something like that
danpost danpost

2017/2/16

#
Nosson1459 wrote...
(1) Why is the Locale part needed, I get the same results with <Code Omitted >? (2) System.currentTimeMillis() is the same as Calendar.getTimeInMillis()? (3) The Calendar.getTime() is the same as Date today = new Date()
(list items numbered for convenience by danpost) (1) Not needed. Just optional FORMAT locale instead of default. (2) Yes. (3) Yes, again.
Nosson1459 Nosson1459

2017/2/17

#
OK. If somebody lives somewhere else and it is or isn't using this locale thing, what time will it say on his screen mine or his?
danpost danpost

2017/2/17

#
Nosson1459 wrote...
OK. If somebody lives somewhere else and it is or isn't using this locale thing, what time will it say on his screen mine or his?
It will show the system date/time of whatever system was running the program. Mine will say mine and yours will say yours (provided the times were properly set for our locales on our systems).
Nosson1459 Nosson1459

2017/2/17

#
danpost wrote...
It will show the system date/time of whatever system was running the program. Mine will say mine and yours will say yours (provided the times were properly set for our locales on our systems).
That's without the Locale parameter being set? And then when it is set it will show a specific time for a specific place no matter where the program is running?
danpost danpost

2017/2/17

#
Nosson1459 wrote...
That's without the Locale parameter being set? And then when it is set it will show a specific time for a specific place no matter where the program is running?
The locale parameter is for the date/time text formatter -- not for the date/time itself. It is for the format and the language that the system date/time is set up and given in.
Nosson1459 Nosson1459

2017/2/17

#
So it doesn't make much difference basically unless you're expecting to see a different format and you get mixed up. What's the default format?
There are more replies on the next page.
1
2