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

Why: new Date()

Hi guys,

Okay, I'm just wondering why you can't use '1421849732000L' for the date?

I understood the instantiation (? Sorry, not very good with the terms here) of Treet. I was able to figure out the "craigsdennis", "This is the content of the tweet", ..

But why: new Date(1421849732000L); ? What is it about 'Date' that requires the 'new'.

Thanks,

Bart van Ackooij

5 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hey Bart,

In order to create our date object, we have to first tell our program that we want to make is in fact a date object. If we were just to write 1421849732000L then our program would most likely interpret that as a string. However we want that to be converted to a date. That's why we use the Date(1421849732000L), to signify to our program that we want a date object with the time of 1421849732000L. and we use the keyword new because, well, we want to create a new date. the creation of new objects always require the new keyword. when creating a object from a class, remember how we use the new keyword there also? I hope that sort of made sense. Just remember that we always need to use Date() to make a date and input the time of your choice in the parantheses. If you leave it empty, it will make a date object of the current time. and use the new keyword to make a date object

Happy coding and all the best,

Kevin

Kevin Faust
Kevin Faust
15,353 Points

Hey Bart,

You seem to have got the important part down. So when creating our Treet object, it takes 3 parameters. when writing strings/ints/etc. these are all primitive data types as you can see here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

we never use the new keyword for these as these things are built into java and we can just use them freely. the program can recognize them with no problem. however something like date is different. notice how we have to import date at the top? now in order to use the date, we have to create that date object using the new keyword. to sum it up cleaner: if you want to create an object whether it be from a class or from an import, you must always use the new keyword.

Does that make sense? please let me know if your still confused

Kevin

Albert Evangelista
Albert Evangelista
27,689 Points

That long number you see there is a representation of time. I'm not sure if its in seconds though. Anyways, if you pass that number to a new Date(...which is here...), you can use the methods in that Date class to access stuff like month, year, hours ..... seconds. Look in the java Date documentation for more information

Thank you both for replying so quickly.

I did 'get' that 1421849732000L is a representation of the date. I just didn't get the new Date().

I think I get it, but just to be sure.

What you are saying Kevin, is that we need to tell the programm what kind of data we are referring to in the x's in:

Treet treet = new Treet(xxx,xxx,xxx);

Right?

And the programm is able to understand that when it says "craigsdennis", it's a String. And it's able to understand it's a Integer when it says 14. And it's able to understand it's a boolean when it says true. (I tried to add those last two in the workspace we're using, just to see if how it would work, and they both work) But for it to understand Date, it needs: new Date();

Right?

Because otherwise it could be mistaken for a String or an int? Do you happen to know any other classes (classes right?) that need to be explained to the programm like new Date()?

And I still don't understand the 'new' part. I mean, "craigsdennis" is also a new String right? Why wouldn't we say

new String "craigsdennis"

or

new "craigsdennis"

Anyway, thanks for your help so far!

Bart

Benjamin Gooch
Benjamin Gooch
20,367 Points

Bart,

I could be wrong here, I'm not really steeped in java, but I believe the reason you use the new Date() constructor is because we're creating a new object that needs to be referenced. In the future, the program can find that object and pull it's arguments out (the long number) in order to pull that same time/date up. I would assume that if you had several Date() statements in the same class, the program would be overwriting the value in the ( ) without the new constructor to tell it to create a different instance of Date() for reference.

Can anyone confirm or correct my theory for me? I'm still learning as well!

Thanks, Ben

Awesome.

I totally get it now.

Thanks again for your help Kevin.

Kevin Faust
Kevin Faust
15,353 Points

Happy to hear that and no problem!

Happy coding,

Kevin