Java Reference
In-Depth Information
Other formatting options can be specified such as the type of currency symbol or decimal symbol to
use. However, because these options are used less frequently, we will leave you to explore them in the online
documentation for the
NumberFormat
class.
Finally, we need to cover date and time formatting. It cannot be overemphasized how important dates and times
are in business applications. Not only are there many important dates (due date, payment date, birthday, hire date, etc.),
but every transaction that occurs in an organization is documented and stored in a database. A very important piece
of this stored information is the date and time of the transaction. Reports, information retrieval, and editing, are all
performed and generated according to these “date/time stamps.”
Most organizations develop a standard format for all dates and times so that there is consistency across all
reports. In addition, a standard makes programming simpler because the programmer never has to figure out what
format to use. However, there are many different standards; for instance, universal (i.e., 24 hour) time versus HH:MM
am/pm, and some countries have their own standards. In addition, there is a SQL standard for storing dates and times
in a database. Java supports many standards and allows you to create your own display “pattern.” We will first look at
some predefined formats and then show how to define a pattern.
Tutorial: Date and Time Formatting
Java comes with a
Date
class that, when instantiated, contains the current date and time (retrieved from the operating
system). The
Date
object stores the current date and time as the number of milliseconds since January 1, 1970. Not a
very useful number for programmers; however, when displayed, the date is converted into a user-friendly format. The
DateFormat
class has predefined formats that can be used and the
SimpleDateFormat
class allows programmers to
define their own date format patterns. To define a date (like due date or birthday), a
Calendar
object is needed.
1.
Create a new Java class called DateTimeApp with a main method in c7.
In the main method, add the following statements to create and print a
Date
object:
2.
Date d =
new
Date();
System.out.println(d);
Notice that the first line is identified as an error. You must import the
Date
class.
3.
Add the following import statement:
import
java.util.*;
This will also insure the JVM can find the
Calendar
class, which we will use later.
4.
Save and run DateTimeApp as a Java application.
The date and time will be displayed as a three-letter day of the week, three-letter month, two-digit day of the
month, the time in universal format (HH:MM:SS), a three-digit time zone, and a four-digit year. For instance:
Thu Feb 17 14:30:48 EST 2011
Although complete, rarely will the date and time be stored or displayed in this format. To display the current date
or time in a different format, use the
DateFormat
class to get a date formatter or time formatter instance (i.e., object)
for a particular format.
Add the following import statement so that the
DateFormat
class can be accessed:
5.
import
java.text.*;