Start learning with our library of video tutorials taught by experts. Get started

Java Essential Training
Illustration by

Working with date values


From:

Java Essential Training

with David Gassner

Video: Working with date values

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.
Expand all | Collapse all
  1. 10m 8s
    1. Welcome
      1m 3s
    2. Is this course for you?
      5m 35s
    3. Using the exercise files
      3m 30s
  2. 31m 24s
    1. The history of Java
      5m 19s
    2. Java compilation and syntax
      8m 54s
    3. Understanding the principles of Java
      8m 28s
    4. Choosing a development environment
      8m 43s
  3. 19m 5s
    1. Installing Java on Windows
      6m 42s
    2. Installing Eclipse on Windows
      3m 19s
    3. Exploring Java on Mac OS X Leopard and Snow Leopard
      2m 27s
    4. Installing Java on Mac OS X Lion
      3m 27s
    5. Installing Eclipse on Mac OS X
      3m 10s
  4. 46m 10s
    1. Creating a Hello World application
      11m 7s
    2. Exploring the Eclipse IDE
      8m 55s
    3. Compiling and running from the command line
      8m 2s
    4. Passing arguments to the application
      8m 17s
    5. Using the Java API documentation
      4m 5s
    6. Memory management and garbage collection
      5m 44s
  5. 58m 57s
    1. Everything is an object
      5m 59s
    2. Declaring and initializing variables
      9m 15s
    3. Working with numbers
      8m 32s
    4. Converting numeric values
      6m 40s
    5. Understanding operators
      7m 58s
    6. Working with character values
      5m 14s
    7. Working with boolean values
      5m 13s
    8. Outputting primitive values as strings
      5m 33s
    9. Creating a simple calculator application
      4m 33s
  6. 53m 40s
    1. Writing conditional code
      5m 35s
    2. Using the switch statement
      8m 50s
    3. Repeating code blocks with loops
      7m 35s
    4. Creating reusable code with methods
      6m 31s
    5. Declaring methods with arguments
      5m 41s
    6. Overloading method names with different signatures
      5m 53s
    7. Passing arguments by reference or by value
      5m 35s
    8. Creating a more complex calculator application
      8m 0s
  7. 20m 30s
    1. Using the String class
      5m 44s
    2. Building strings with StringBuilder
      3m 34s
    3. Parsing string values
      3m 19s
    4. Working with date values
      7m 53s
  8. 20m 44s
    1. Understanding compile-time vs. runtime errors
      4m 5s
    2. Handling exceptions with try/catch
      4m 55s
    3. Throwing exceptions in methods
      2m 50s
    4. Using the debugger
      8m 54s
  9. 32m 22s
    1. Using simple arrays
      4m 47s
    2. Using two-dimensional arrays
      6m 17s
    3. Managing resizable arrays with ArrayList
      7m 14s
    4. Managing unordered data with HashMap
      6m 5s
    5. Looping through collections with iterators
      7m 59s
  10. 52m 2s
    1. Understanding encapsulation
      5m 59s
    2. Creating and instantiating custom classes
      8m 8s
    3. Organizing classes with packages
      6m 47s
    4. Creating and using instance methods
      6m 52s
    5. Storing data in instance variables
      6m 56s
    6. Using constructor methods
      5m 40s
    7. Managing instance data with getter and setter methods
      8m 26s
    8. Using class variables and Enum classes
      3m 14s
  11. 41m 15s
    1. Understanding inheritance and polymorphism
      9m 12s
    2. Extending custom classes
      9m 1s
    3. Overriding superclass methods
      3m 8s
    4. Casting subclass objects
      5m 3s
    5. Understanding interfaces and implementing classes
      4m 2s
    6. Creating your own interfaces
      4m 14s
    7. Using abstract classes and methods
      6m 35s
  12. 32m 17s
    1. Managing files with the core class library
      7m 46s
    2. Managing files with Apache Commons FileUtils
      7m 32s
    3. Reading a text file from a networked resource
      7m 52s
    4. Parsing an XML file with DOM
      9m 7s
  13. 17m 39s
    1. Creating your own JAR files
      4m 54s
    2. Understanding the classpath
      5m 2s
    3. Documenting code with Javadoc
      7m 43s
  14. 47s
    1. Goodbye
      47s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Java Essential Training
7h 17m Beginner Dec 14, 2011

Viewers: in countries Watching now:

Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.

Topics include:
  • Understanding the history and principles of Java
  • Installing Eclipse and Java
  • Compiling and running from the command line
  • Managing memory and performing garbage collection
  • Declaring and initializing variables
  • Writing conditional code
  • Building and parsing strings
  • Debugging and exception handling
  • Using simple arrays
  • Creating custom classes
  • Working with encapsulation, inheritance, and polymorphism
  • Managing files
  • Documenting code with Javadocs
Subjects:
Developer Programming Languages
Software:
Android Java Eclipse
Author:
David Gassner

Working with date values

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.

There are currently no FAQs about Java Essential Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Java Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.