|
June 2003 Technical Tip – Processing Dates in Java
Java provides plenty of functionality for processing dates: you just
have to know where to look! This month we will look at a simple
application which accepts a date from the user, verifies that the user
did, in fact, give us a valid date, and then shows that date in a
consistent format. First, let's take a quick look at the classes we will
be using in this application (comments from the API are shown in
italics):
- java.util.Date
- The class
Date represents a specific instant in time,
with millisecond precision. As of JDK 1.1, the Calendar
class should be used to convert between dates and time fields and the
DateFormat class should be used to format and parse date
strings. The corresponding methods in Date are
deprecated. Our objective is to create an instance of a Date.
- java.text.SimpleDateFormat
- A concrete class for formatting and parsing dates in a
locale-sensitive manner. This is a subclass of the DateFormat class.
We will use SimpleDateFormat for formatting and parsing dates (parsing
is converting from a String to a Date.)
- java.text.ParseException
- Signals that an error has been reached unexpectedly while
parsing. Thrown when an invalid date is detected. For more
information on Exceptions, see our March 2003 article.
Here's our source code, DateDemo.java, with plenty of embedded
comments:
// Demo date validation and formatting.
// Written by Bill Qualls.
// (c) 2003 by Caliber Data Training 773.794.1222
import java.io.*; // for input from keyboard
import java.util.Date; // our objective is a Date
import java.text.SimpleDateFormat; // for parsing and formatting
import java.text.ParseException; // thrown on invalid date
public class DateDemo
{
public static void main(String[] args)
throws IOException
{
// take input from keyboard in this example
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
// indicate expected format of dates. Note capital MM.
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
// new Date() gets today's date. Show in the desired format.
System.out.println("Today is " + df.format(new Date()) );
// not lenient, otherwise 02/29/2003 becomes 03/01/2003!
df.setLenient(false);
// get a date from the user, show them the expected format
System.out.print("Enter a date (" +
df.toPattern().toLowerCase() + "): ");
String s = keyboard.readLine();
// see if it's a valid date; if so then show it
try
{
Date d = df.parse(s);
System.out.println("Good date! " + df.format(d));
}
catch (ParseException pe)
{
System.out.println("Invalid date.");
}
}
}
| Download source code
here.
Sample output follows:
C:\Java\MyJava>java DateDemo
Today is 04/28/2003
Enter a date (mm/dd/yyyy): 02/29/1995
Invalid date.
C:\Java\MyJava>java DateDemo
Today is 04/28/2003
Enter a date (mm/dd/yyyy): 02/29/1996
Good date! 02/29/1996
C:\Java\MyJava>java DateDemo
Today is 04/28/2003
Enter a date (mm/dd/yyyy): 3/1/1996
Good date! 03/01/1996
|
Caliber Data Training is pleased to offer training in Java programming.
We have recently rewritten our Visualage Java course to use WebSphere
Studio Application Developer. We have also created a new Intermediate Java
Programming course. We hope you will consider us when deciding upon a
training provider!
Go to the articles index. Written
by Bill Qualls. Copyright © 2003 by Caliber Data Training
800.938.1222 |