Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Java gives you a couple of different ways of representing dates just as with strings, the date is a complex object and not a primitive variable. I'm working in this empty main method in the Dates project and I'll start by declaring an instance of this simple date class. I'll type in the word Date. Now this is going to be the data type of my variable, but unlike all the other data types that I've have used previously, the date class is not a member of a packaged called Java.lang, which is always available to your code and to the runtime environment. Now we are stepping out into other libraries in the core Java class library, specifically the date class is a member of a package called java.util, it's a part of the core class library, it's always available to you.
But if you want to use this class in your code you have to explicitly import it into your code. Eclipse will help you with the syntax, with the cursor right after the word Date press Ctrl+Space. You'll see four different date classes offered. The one we want is at the top java.util. There are other date classes for java.sql and others for sun.util. I'll press Enter or Return and that adds the import statement for that date class into my code at the top. I'll give the variable a name of d, and then I'll create the instance using the new keyword and then the constructor method, which like all constructor methods matches the name of the class and then with the parentheses in place.
I'll press Ctrl+Space. You'll see that there are a number of versions of the constructor method for the date class. If you use what we call the no arguments constructor, that is the method that you don't pass any values into, you'll create an object that represents the current date and time on your computer. The next version takes a long integer, representing the number of milliseconds, since the epoch date. January 1, 1970 at midnight, you can also represent a date as a string, as three integer values representing the year, month and date, and you can get all the way precise down to seconds.
I am going to choose the no arguments constructor. I'll finish that statement with a semicolon and then I'll output the value of the date object using System.out. println and I'll just pass the date object in. The date class has a two string method, when you pass in the object to println, just like all variables in Java, when you pass the object in, you'll get a string representation that's determined by that two string method. I'll run the application and I'll get the default formatting. The day and month represented as three character alpha values, the date, the time, the time zone and the year.
Now the other class that you can use to represent a date in Java is called Gregorian calendar. The name of the class has calendar in it but it really represents a particular date and time. You use very similar syntax to instantiate a Gregorian calendar object. Because Gregorian calendar is a long name, I'll just type in the beginning, Greg, and I'll press Ctrl+Space and I'll let Eclipse fill in the rest of the class and add the required input statement at the top. I'll name this new variable gc for Gregorian calendar.
For the constructor method, once again, I'll type the beginning of the class name and press Ctrl+Space. You'll see that there are constructor methods for this class, including some that match the style of the date class, there's no arguments constructor method, there is a version that takes a year, month and day, one that goes all way down to seconds and then there are particular versions of the calendar class that allow you to set the locale and time zone. I'm going to use this version that lets me set a particular year, month and day. When you set the year, you need to pass in a four digit numeric value.
I'll type a value of 2009, set the month to a value from 0 to 11 just like a Java array, a month is represented using zero-based calculations. So 0 is January, 1 is February and so on. I'll type the number 1 for February and then I'll set the day of the month at 28, the last day of February in that year. Now if I were to type in a value of 29, the Gregorian calendar class is smart enough to know that 2009 was not a leap year and it would reject that. One of the advantages of using a Gregorian calendar object is that you can do math with it.
You can add, subtract and otherwise manipulate the date. I'm going to increment the value of the day in the Gregorian calendar by 1. So I should go from February 28 to March 1. Here is the code, gc.add. The add method takes two values, a field which represents which part of the calendar I want to manipulate, and the amount. For the field use a value that's a member of the Gregorian calendar class, like this. I'll type in GregorianCalendar and then dot and I'll see a listing of all of the available settings that represent parts of a date.
And I am going choose this one, DATE which represents the day. And then I'll tab over and set the value as 1. So I am adding 1 day to the date. Now to output the value, I have to convert it back to a date object. The Gregorian calendar doesn't have formatting capability, it only has math, manipulation and the ability to breakdown the date into small parts. So in order to get it ready for formatting, I'll create another new date class. I'll call this one d2, and I'll get its value from gc.getTime.
The getTime method returns an instance of the date class. Now for formatting, I'm going to use yet another class called DateFormat. I'll create an instance of the DateFormat class, just as with date in Gregorian calendar, I need an import, so I'll type in the name of the class and press Ctrl+Space and I'm going to choose the DateFormat class from the package java.txt. I'll call my DateFormat class df for date format. Now in order to construct an instance of this class, you don't use the new keyword; the DateFormat class has a number of methods that you can use to return instances of the DateFormat class.
I'll use this syntax, DateFormat.getDateInstance. This is a particular design pattern called a factory method. A factory method of a class knows how to return an instance of that class, and you'll see in this list many different factory methods that behave differently. Again, for detailed information about what each of these methods does, look at the docs. I'll choose this very first factory method, getDateInstance. Next, I'll create a string and I'll call this one sd for a string of a date.
And I'll get the value of sd from df. format, and I'll pass in the d2 variable, the date object I got from the calendar. And then finally, I'll output the value using System.out.println and pass in the value of sd. I'll run the code and there is the result. I started off with February 28th, I added a value of 1 to the date and I got March 1, 2009. Now this is the default formatting. You can easily manipulate or change how the formatter behaves. I'm going to change the way I called to getDateInstance method.
I'm going to pass in a DateFormat.FULL. This is a field or a constant of the date format class that means use more extended formatting. I'll Save and Run the application again and now I get the full day, month, date and year. So that's a look at three useful classes that you can use to manage and manipulate dates. The date class which represents the date as the number of milliseconds, since January 1, 1970, through Gregorian calendar that lets you break down the date into parts and do math, and otherwise manipulate the values, and the date format class that allows you to format the date for presentation.
All three of these classes require explicit imports in your code, because none of them are members of that package java.lang, these classes are always available. I'll talk more about import statements later on in the course, but just know, if you're not sure whether you need an import statement for a class, just place the cursor after the class name, press Ctrl+ Space and select it and if you need it, Eclipse will add it to the code for you.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99451 Viewers
56 Video lessons · 112689 Viewers
71 Video lessons · 81507 Viewers
131 Video lessons · 39128 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.