The Date class began to evolve from the Java Development Kit (JDK) 1.0. At that time, it only included several methods to obtain or set various parts of a date data, such as month, day, and year. These methods are now criticized and have been transferred to the Calendar class, which we will discuss further in this article.
This improvement is designed to better process internationalized formats for date data. Just like in JDK 1.1, the Date class is actually just a wrapper class, which contains a long integer data, representing the number of milliseconds experienced before or after the moment of 00:00:00 on January 1, 1970.
Create a date object
Let's look at a simple example of creating a date object using the current date and time of the system and returning a long integer. This time is often called the system time of the Java Virtual Machine (JVM) host environment.
import ;
public class DateExample1 {
public static void main(String[] args) { //Replace yourself []
// Get the system date/time
Date date = new Date();
(());
}
}
On Saturday, September 29, 2001, it was about 6:50 in the afternoon. The above example shows the result on the system output device 1001803809710. In this example, it is worth noting that we used the Date constructor to create a date object, which does not accept any parameters. This constructor internally uses the() method to obtain dates from the system.
So, now we know how to get the milliseconds that have gone through from January 1, 1970. How can we display this date in a format that the user understands? Here the class and its abstract base class come in handy.
Customized format for date data
If we want to customize the format of date data, for example Saturday - September - 29 - 2001. The following example shows how to do this:
import ;
import ;
public class DateExample2 {
public static void main(String[] args) { //Replace yourself []
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
Date date = new Date();
((date));
}
}
By passing the format string "EEE-MMMM-dd-yyyy" to the SimpleDateFormat constructor, we can indicate the format you want. You should see that the ASCII character in the format string tells the formatting function which part of the date data is displayed below. EEEE is the week, MMMM is the month, dd is the day, and yyyy is the year. The number of characters determines how the date is formatted. Passing "EE-MM-dd-yy" will display Sat-09-29-01. Please see the Sun Web site for the complete instructions for the date formatting options.
Parse text data into date objects
Suppose we have a text string containing a formatted date object, and we want to parse this string and create a date object from the text date data. We will call the SimpleDateFormat class again with the format string "MM-dd-yyyy", but this time, we use format parsing instead of generating a text date data. Our example, shown below, will parse the text string "9-29-2001" and create a date object with a value of 001736000000.
Example program:
import ;
import ;
public class DateExample3 {
public static void main(String[]args) { //Replace yourself []
// Create a date formatter that can parse dates of
// the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001";
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date = (dateStringToParse);
// Now send the parsed date as a long value
// to the system output.
(());
}
catch (Exception ex) {
(());
}
}
}
Use standard date formatting procedures
Now that we can generate and parse customized date formats, let's take a look at how to use the built-in formatting process. Method () allows us to obtain the standard date formatting process in several different ways. In the following example, we obtain four built-in date formatting processes. They include a short, medium, long, and full date format.
import ;
import ;
public class DateExample4 {
public static void main(String[] args) { //Replace yourself []
Date date = new Date();
DateFormat shortDateFormat = (,);
DateFormat mediumDateFormat = (,);
DateFormat longDateFormat = (,);
DateFormat fullDateFormat = (,);
((date));
((date));
((date));
((date));
}
}
Note that we pass two values in each call to getDateTimeInstance. The first parameter is date style, while the second parameter is time style. They are both basic data type int (int). For readability considerations, we use constants provided by the DateFormat class: SHORT, MEDIUM, LONG, and FULL. For more methods and options for getting the time and date formatting process, see the explanation on the Sun Company Web site.
When running our example program, it will output the following content to the standard output device:
9/29/01 8:44 PM
Sep 29, 2001 8:44:45 PM
September 29, 2001 8:44:45 PM EDT
Saturday, September 29, 2001 8:44:45 PM EDT
This improvement is designed to better process internationalized formats for date data. Just like in JDK 1.1, the Date class is actually just a wrapper class, which contains a long integer data, representing the number of milliseconds experienced before or after the moment of 00:00:00 on January 1, 1970.
Create a date object
Let's look at a simple example of creating a date object using the current date and time of the system and returning a long integer. This time is often called the system time of the Java Virtual Machine (JVM) host environment.
import ;
public class DateExample1 {
public static void main(String[] args) { //Replace yourself []
// Get the system date/time
Date date = new Date();
(());
}
}
On Saturday, September 29, 2001, it was about 6:50 in the afternoon. The above example shows the result on the system output device 1001803809710. In this example, it is worth noting that we used the Date constructor to create a date object, which does not accept any parameters. This constructor internally uses the() method to obtain dates from the system.
So, now we know how to get the milliseconds that have gone through from January 1, 1970. How can we display this date in a format that the user understands? Here the class and its abstract base class come in handy.
Customized format for date data
If we want to customize the format of date data, for example Saturday - September - 29 - 2001. The following example shows how to do this:
import ;
import ;
public class DateExample2 {
public static void main(String[] args) { //Replace yourself []
SimpleDateFormat bartDateFormat = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
Date date = new Date();
((date));
}
}
By passing the format string "EEE-MMMM-dd-yyyy" to the SimpleDateFormat constructor, we can indicate the format you want. You should see that the ASCII character in the format string tells the formatting function which part of the date data is displayed below. EEEE is the week, MMMM is the month, dd is the day, and yyyy is the year. The number of characters determines how the date is formatted. Passing "EE-MM-dd-yy" will display Sat-09-29-01. Please see the Sun Web site for the complete instructions for the date formatting options.
Parse text data into date objects
Suppose we have a text string containing a formatted date object, and we want to parse this string and create a date object from the text date data. We will call the SimpleDateFormat class again with the format string "MM-dd-yyyy", but this time, we use format parsing instead of generating a text date data. Our example, shown below, will parse the text string "9-29-2001" and create a date object with a value of 001736000000.
Example program:
import ;
import ;
public class DateExample3 {
public static void main(String[]args) { //Replace yourself []
// Create a date formatter that can parse dates of
// the form MM-dd-yyyy.
SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy");
// Create a string containing a text date to be parsed.
String dateStringToParse = "9-29-2001";
try {
// Parse the text version of the date.
// We have to perform the parse method in a
// try-catch construct in case dateStringToParse
// does not contain a date in the format we are expecting.
Date date = (dateStringToParse);
// Now send the parsed date as a long value
// to the system output.
(());
}
catch (Exception ex) {
(());
}
}
}
Use standard date formatting procedures
Now that we can generate and parse customized date formats, let's take a look at how to use the built-in formatting process. Method () allows us to obtain the standard date formatting process in several different ways. In the following example, we obtain four built-in date formatting processes. They include a short, medium, long, and full date format.
import ;
import ;
public class DateExample4 {
public static void main(String[] args) { //Replace yourself []
Date date = new Date();
DateFormat shortDateFormat = (,);
DateFormat mediumDateFormat = (,);
DateFormat longDateFormat = (,);
DateFormat fullDateFormat = (,);
((date));
((date));
((date));
((date));
}
}
Note that we pass two values in each call to getDateTimeInstance. The first parameter is date style, while the second parameter is time style. They are both basic data type int (int). For readability considerations, we use constants provided by the DateFormat class: SHORT, MEDIUM, LONG, and FULL. For more methods and options for getting the time and date formatting process, see the explanation on the Sun Company Web site.
When running our example program, it will output the following content to the standard output device:
9/29/01 8:44 PM
Sep 29, 2001 8:44:45 PM
September 29, 2001 8:44:45 PM EDT
Saturday, September 29, 2001 8:44:45 PM EDT