Java: Date Data Type

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Unsigned Data Types

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Java Date
  • 1:42 Examples
  • 4:44 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

A Date in Java is more than just a data type. It's also a class, with its own methods available. This lesson will provide an overview and syntax of Java Dates.

Java Date

The Date in Java is not only a data type, like int or float, but a class. This means it has its own methods available for use.

A Date in Java also includes the time, the year, the name of the day of the week, and the time zone. One of its methods includes extracting the time from the Date object.

Note: In order for this to work, you need to make sure to bring in the Java Date package. At the start of the program, use the import command to do this code:


Java Date Package


Next, let's declare a new date. Since the Date is a class, we need to create a new object of the class by using the new keyword when declaring the variable. The syntax below declares a new Date data type/object. This basic statement will default to the current date.


Java Date Basic


If we display the output of this variable as is, it might look something like Mon Jul 18 20:25:48 CDT 2016. Here we see the time as well as the local time zone.

Let's pause for a second to make note on Java years, months, and days. Java stores the parts of the date a little differently than you'd think. In one of our examples appearing in this table below, we hard-code the date to 2017, but the value we put in is 117. This is because Java saves the years as an integer value of x - 1900. Therefore 2017 - 1900 equals 117. The table below shows other important information regarding the date parts:

Date Part Java Value Notes
Year x - 1900 ex: 2018 = 118; 2018-1900 = 118
Month integer from 0 to 11 January = 0; February 1, etc.
Day integer from 1 to 31 Normal representation
Hour integer from 0 to 23 Midnight is 0
Minute integer from 0 to 59
Second integer from 0 to 61 60 and 61 are used for leap calcualtions

Examples

A very common programming function is the comparison of dates. Before we can show this comparison, however, we need to understand how Java stores dates.

1. Example: Seconds Since 1/1/1970

Java keeps time from January 1, 1970, storing this as the number of milliseconds since that date. As such, this value is a long data type. Why would this number be of interest?

Working with dates can create some challenges to the programmer. How many days are there between March 5, 1983 and June 7, 1995, for instance? If you simply subtracted these dates, you'd probably get a wrong answer, due to leap years. Because we have the number of milliseconds since 1/1/1970, the calculations are much more precise.

In order to get this number, we can invoke the getTime() method within the Date class. From our previous example, let's get the current number of milliseconds:


Java Date Get Time


To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support