Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Getting There Class Review

Aditya Puri
Aditya Puri
1,080 Points

Dont understand something

When he instantiated the treet class-

1) One of the parameters were "new Date". I don't know how this makes sense.

2) I don't really understand the java.util.Date package. What does it do? And which method are we using?

1 Answer

Harry James
Harry James
14,780 Points

Hey Aditya,

When we create a new [class], we can think of it as creating a new [object] (After all, a Class is an Object). So in the case of creating a new Date(), we are creating a new date object which will set, or initialize itself to the time when that line of code runs. This means later on, we can work with the date that is set. A thing to note is that the time does not automatically change. If you initialized the date/time and logged it to the console 10 seconds later, the time printed would be 10 seconds early.

The java.util.Date package is a package that allows us to access the Date class/object. Without it, Java won't understand what we're trying to talk about when we create a new Date(). To the compiler, it will think that we have our own Date class somewhere, which we don't, so we'll get an error. However, if we tell the compiler we're using the Date class from the java.util package, it will go "Ok! Got it!" and let us use the class without any errors :)

When you say which methods we are using, by importing the Date from the java.util package, we get access to all of the methods shown on this page. These are all methods that can be called off the class (We do this by putting a period after the class followed by the method we want to call) but again, if we didn't have this class in the first place, the compiler wouldn't be able to tell what methods exist on that class.


Hopefully this should help explain things, but if you have any more questions, give me a shout :)

Aditya Puri
Aditya Puri
1,080 Points

so we are assigning a "new Date" to the variable "currentTime" of the type Date? We are making the variable refer to a new object that we create during the instansiation?

Harry James
Harry James
14,780 Points

You got it Aditya!

We are assigning a "new Date" to the variable "currentTime". Therefore, currentTime is of the type Date. And yes, this variable is being set to the object we made when we instantiated it (The moment in time when we give currentTime a value).

Just to give a bit more sense to this, we call it instantiating because we can have multiple, different Date classes at a time. It would be a pain if whenever we instantiated a new Date object, all of the other dates were set to the same moment in time. Therefore, we can have multiple instances of a class, and each Date object can have its own unique time.

Great work :)

Aditya Puri
Aditya Puri
1,080 Points

1) What do we mean when we add the "346346234667L" in "new Date(346346234667L)" ?

2) I don't understand this part-' thing to note is that the time does not automatically change. If you initialized the date/time and logged it to the console 10 seconds later, the time printed would be 10 seconds early.'

Harry James
Harry James
14,780 Points

Hey again Aditya,

Not sure where you got the 3463... number from, but I believe it would be a time in UNIX that you're talking about.

For example, in this video when Craig creates a Date using a similar number (1421...), he uses what's called a UNIX Timestamp.

Basically, a UNIX timestamp counts the number of seconds that have passed since January 1st, 1970, 00:00:00GMT, and uses this to work out the date. So, at the time of writing, 1472916935 seconds have passed since that time, and that corresponds to 03 September 4:35PM GMT


Now, the idea that the time does not change dynamically is quite important to understand the language of Java.

When we initialize the time using new Date(), it gets the UNIX time of this current moment in time. However, that number doesn't just continue ticking up in seconds. So, here's an example to better understand this:

long currentTime = new Date();
// The time when this code runs is 1472916935.
console.log(currentTime); // Outputs 1472916935.

Thread.sleep(10 * 1000); // Pause code execution for 10 seconds.
console.log(currentTime); // Outputs 1472916935.

So, as you can see, the currentTime is still the same! Huh. How would we get around this? Well, we'd need to call Date's constructor for a second time, and get a new Date object returned back:

currentTime = new Date();
// The time when this code runs is 1472916945.
console.log(currentTime); // Outputs 1472916945.

Now, we've called the Date() constructor for a second time, and it's returned the up-to-date date/time so that we can output it.


Hopefully this explains things but if you still don't fully understand, let me know :)

Aditya Puri
Aditya Puri
1,080 Points

oh..but I have 3 more questions :p -

1) How does the instantiation of the class Date, set it equal to the current date? Is it because of some code in the constructor?

2) When we write "new Date" then we set the date object equal to the time when the program was compiled, or when the user posted the forum post, and there is an automatically number generated in the pranthesis()?

3) When we give a number in the paranthesis as an argument, like "new Date(564564757567L)" then will it work out a date using the same method? Just this time, the date would be pre feeded and not the current date, right?

Harry James
Harry James
14,780 Points

Hey again Aditya,

For your first question, you're correct - it is because of some code in the constructor. This code gets the current time, and then returns it as a long in the constructor.

When we pass in a date to the constructor, it will run a constructor that can handle this date, and store it in the Date object created. Note that if we did new Date() with 0 arguments, it uses a different constructor that gets the current time instead. Also, this is the runtime (The time this line of code is run in program) - not the compile time.

This should also answer your last question but just to reiterate; yes, passing an argument causes a different constructor to run that can handle the argument, and that will use the time we pass as the argument.


Hope it helps and if there's anything else, give me a shout :)

Aditya Puri
Aditya Puri
1,080 Points

when we declare "new Date()" in our program, it will feed the current time when the program runs. When we use "new Date()" in an input-output base program it will feed the time when the user inputted the data and when we use "new Date()" in an if statement, it will feed the time when the statement was true, right?

Harry James
Harry James
14,780 Points

Hey again Aditya,

The Date is created whenever that specific line of code is executed. So, if this is inside an if statement, it would be when it is true, as that is when the code gets executed. This applies for all of the examples you have stated - whenever the line of code is executed is when the Date gets set :)

So, yes, if you did have an i/o system, you would wait for the input and if the input triggers a new Date object to be created, then the date would be set to the time of input.

Aditya Puri
Aditya Puri
1,080 Points

oh, ok thanks :D