The Date class

To understand the concept of the Date class, we will begin by creating source code for our dateDemo class. Let's say we want to print the current date or current time. How do we print that?

At times, we are asked to enter the date into the current date field and we need to get it from Java. In such cases, we will use the Date class, which will give us the current date and current time, in seconds as well. So every detail about the day, week, month, year, or hour can be read through from Java classes. Java has developed a class called Date, from which we can get all these details. The following screenshot displays the source code:

Source code displaying use of date class

Basically, we need to use the methods present in that particular class. To use the methods present in that class, we need to create an object of that particular class. For this, let's consider the following code syntax:

Date d= new Date();

This Date class comes from the util package, and d is the object of the Date class where date and time are present. In the previous chapter, we saw that Java has some packages, such as the java.lang package which traps all fundamental Java stuff, and java.util, which is where we have collection framework and the Date class.

The preceding code syntax says that we do not know where the Date class is. To make this class available to our Java file, we need to import the util Java package because this Date class is packed into that particular package. If we use it to import the package into the preceding class, you can use that date successfully. Move your mouse here and it says import 'Date' (java.util), as shown in the following screenshot:

Quick fixes drop down with suggestions to correct the code error

Once you click on that, you will see:

import java.util.Date

Where util is the package, and Date is a class.

As we have seen, d is the object that contains the date and time, but how do we print it? Because it is an object format, we cannot simply use the following:

System.out.println(d)

To convert it into readable text, refer to the following screenshot:

Converting the code into readable text format

Here, we are converting Date into a string so that we can visually see it in our output. On running the preceding code as shown in the screenshot, it prints the following:

Fri Apr 15 17:37:27 EDT 2016

This is how we can print the entire date, time, and month from our current system's Java date. The format in the preceding output is not what we generally get, but it might be in a specific format, such as:

mm//dd//yyyy

If we want to extract our date in the preceding format, how do we do that? 

The d object gives us all the details. But how can we convert all these details into the preceding format? For this we will use the following:

       Date d= new Date();

SimpleDateFormat sdf=new SimpleDateFormat("M/d/yyyy");
System.out.println(sdf.format(d));
System.out.println(d.toString());

 The output of the preceding code syntax will be:

 Output displaying date and time as per the code

Refer the following URL for the SimpleDateFormat format code:

Now, on altering the object and the SimpleDateFormat code, we see the following:

 Date d= new Date();

SimpleDateFormat sdf=new SimpleDateFormat("M/d/yyyy");
SimpleDateFormat sdf=new SimpleDateFormat("M/d/yyyy hh:mm:ss");
System.out.println(sdf.format(d));
System.out.println(sd.toString());
System.out.println(d.toString());

The output will be:

Output displaying the date and time in a new format

Thus, we can actually format our date as per our requirements and pass that into the SimpleDateFormat method. We can bring the d object and place it into an argument so that it will be formatted in a particular manner. This is how dates are retrieved using Java. 

In the next section, we will see how to use the Calendar class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.236