TechNet Home Page   All Products  |   Support  |   Search  |   microsoft.com Home  
Microsoft
  TechNet Home  |   TechNet CD Online  |   Events  |   International  |

Search TechNet
Advanced Search

 Features

 Hot Topics
Year 2000
Euro
Intranet
Commerce

 Technical Information

 Community

 IT Scenarios

 Managing IT

 TechNet CD-Rom

Microsoft Year 2000 Readiness Disclosure & Resource Center
Building Year-2000 Compliant Applications with Visual Studio and Windows DNA
5. Listing 1 – Validation in Java

The SimpleDateFormat class in Java is used to format and parse date and time data. It can be used to convert a string to a date, or a date back to a string, and format it with the specified format mask. You can specify a formatting mask to use during the constructor:

//Specify the date format that we're expecting.
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
df.setLenient(false);

Listing 2 – Using SimpleDateFormat

This indicates that whenever we format a date using this instance of the SimpleDateFormat class, Java will use the format "MM/dd/yyyy". This causes the date May 31st, 1998 to be formatted as 5/31/1998. Remember that Java is a case sensitive language. Using the lower case letter "m" instead of upper case will not produce the same results – the lower case letter "m" is used for the minutes portion when specifying a formatting mask for a time.

The second line in Listing 2 tells the SimpleDateFormat class not to be lenient when interpreting dates. It is important to tell the SimpleDateFormat class not to be lenient when handling dates. If you don't, Java will not verify that the data entered is actually a valid date, rather it will only verify that it is in the proper format. For example, the invalid date in Listing 3 will not generate an exception because the format of the date is valid. The default value for setLenient is true, so always change it to false when working with the SimpleDateFormat class.

//This will not generate an exception, even though the date is invalid.
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date dt = new Date();
dt = df.parse("02/29/1997");

Listing 3

In order to verify that the dates entered by the user are valid, we need to convert them into Java's Date data type. We can do this by calling SimpleDateFormat's parse() function. The parse() function takes a string and attempts to convert it into a Date data type. If the string cannot be converted to a date, an exception will be raised which we can trap and use to return an error message, as displayed in Listing 3:

//Attempt to create a date from the text entered by the user.
try {
transDate.setTime(df.parse(m_txtDate.getText()));
}
catch (Exception e) {
//If the user entered an invalid date, return an error message
return "The Transaction Date is invalid. Proper format is 'mm/dd/yyyy'";
}

Listing 4 – Validating the dates

This type of procedure should be followed with any other dates entered by an end user. By attempting to convert string data stored in the applet's TextField into Java’s native Date data type, we are assured that the information entered is in fact a valid date.

Now that we know all of the date information is valid, the applet will enforce one business rule: the due date cannot be earlier than the transaction date. In order to do this, we need to compare two dates. When comparing dates or performing date arithmetic, always use the development tool’s operators. In Java, the SimpleDateFormat class is used for formatting and parsing only. The class used for date arithmetic and comparisons, including time zone conversions, is the Calendar class (java.util.Calendar). The Calendar class can be used to extract a certain portion of a date (such as the month or the year) or to perform comparisons on two dates, as displayed in Listing 5.

transDate.setTime(df.parse(m_txtDate.getText()));
dueDate.setTime(df.parse(m_txtDueDate.getText()));
//make sure due date is the same or later
//than the transaction date...
if (transDate.after(dueDate)) {
return "The Due Date cannot be earlier than the Transaction Date.";

Listing 5 – Validating the dates

Before using the Calendar class, we must assign it a date. We do this with the setTime() function. This function assigns a date, a time, or both to the Calendar. The setTime() function takes a date as its argument. We previously parsed the contents of the TextFields into dates for the Transaction Date and Due Date when we were validating those fields. We can move the results into their respective Calendars at the same time.

The Calendar’s after() function, which also takes a Calendar is a parameter, returns true if the date or time of the Calendar passed as a parameter is after the date or time of this Calendar. Comparing the two Calendar objects, then, is just a matter of calling the after() function on the Transaction Date's Calendar and passing in as a parameter the Due Date's Calendar. If it is false, we will return an error message; otherwise we return an empty string and processing continues. By using the Calendar class's built in date comparison functionality, we’ve removed all possibility of programmer date math bugs or lack of precision.

Once the data has been validated by calling the applet's CheckData() function, we are ready to send it to the server to be further processed and eventually saved to the database. We'll get the data from the applet to the web server through the use of an HTML POST method. The HTML POST method is used to send data from a form tag on a web page to the server to be processed. In our example, all of the inputs on the form are of type hidden, which means the browser will not make them visible to the user. We can still store values in the hidden fields, then send them to the server by using POST.

To get the values from the applet to the HTML form, we call the applet's Customer(),Date(), Amount(), Terms() and DueDate() functions and copy the values returned from those functions into the form fields. Listing 6 shows the JavaScript function add() which was taken from the Active Server Page that gets the values from the applet. This is the code that gets called when the user clicks the Add button on the client page (refer to Figure 1).

function add() {
var sRet = document.Y2kApplet.CheckData();
if (sRet != "") {
alert(sRet);
return false;
}
else {
with(document.forms[0]) {
hidCustomer.value = Y2kApplet.Customer();
hidDate.value = Y2kApplet.TransDate();
hidAmount.value = Y2kApplet.Amount();
hidTerms.value = Y2kApplet.Terms();
hidDueDate.value = Y2kApplet.DueDate();
submit();
}
}

<< 1 2 3 4 5 6 7 8 >>


Send This To a Friend Download This Article


 

Thursday, March 11, 1999
© 1998 Microsoft Corporation. All rights reserved. Terms of use.

This site is being designated as a Year 2000 Readiness Disclosure and the information contained herein is provided pursuant to the terms hereof and the Year 2000 Information and Readiness Disclosure Act.