When developing applications about calendars, the number of irregular monthly dates, days of the week, and weekend calculations has always been a no-brainer. As a result, date and time programming often makes novice programmers cringe. This article will demonstrate the use of dates in JSP applications to provide a convenient reference for readers to complete some common tasks.
Objective
The purpose of the sample application is to:
Submit a date parameter to a JSP page with an HTML form.
Receives a date parameter and creates a calendar object.
Use the Calendar object to find the date of the week and how many working days are in the selected month.
Format dates in a display format that is easy for users to read.
Environment
The sample program code is tested on a Java Web server that has JDK 1.31 configured. The example is very standard and works almost exactly the same on Tomcat or other JSP Web servers (JDK 1.2 or later).
Pass a date to a JSP page through a form
The date (date) parameter is selected by the user from the 3 column Drop-down list. After the user submits the form, the parameters are sent to the processing page.
Do not forget to import Java.util.Calendar when you are programming your calendar:
〈%@ page import= "Java.util.Calendar"%〉
The 1th task for processing a page is to receive the following date parameter values: date, month, and year.
int curdate = 1;
if (Request.getparameter ("curdate")!= null)
{
Curdate = Integer.parseint (Request.getparameter ("curdate"));
}
Note that the page parameters are converted to type int, and we'll see why we're doing this.
Calendar Object
Our goal is to create and set up a calendar object to use for date calculations. To do this, we first need to instantiate a calendar object.
Calendar cal = Calendar.getinstance ();
Calendar.getinstance () Returns a Calendar object that represents the current date and time.
Cal.clear ();
Cal.set (Curyear, Curmonth, curdate);
The clear () method clears the calendar so that we can assign our own date values to the object and prepare for future computations. Note The order of these parameters: First is the year, and the last is the date.
Get information from Calendar
The following is a set of calendar fields:
Date
DATE, Day_of_month, Day_of_week, day_of_year
Time
Hour_of_day, MINUTE, millisecond, SECOND
Week
Week_of_month, Week_of_year
Years
Year
All of these fields can be accessed through the calendar's Get () method, and the result returns an integer. The following code example shows the above procedure.
Date in a week
int dayofweek = Cal.get (cal. Day_of_week);
Out.print ("〈br〉day of Week:" + DayOfWeek + "〈br〉");
Dates in January
int dayofmonth = Cal.get (cal. Day_of_month);
Out.print ("〈br〉day of Month:" + dayofmonth + "〈br〉");
Locate a specific date
To find a specific day of the week, you must access the Day_of_week field. The field contains an integer value ranging from 1 to 7, 1 for Monday, and 2 for Tuesday, and the rest for the second analogy.
int dayofweek = Cal.get (cal. Day_of_week);
Here's a good way to show a date to a user, which is to declare an array that contains the number of days in a week. You can then easily display the various dates. Just use the Day_of_week integer to access the current date within the array.
String[] weekdays = new string[]
{"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
〈%=weekdays[cal.get (Cal. Day_of_week)]%〉
Note that the 1th element of the array is empty. This is because the range of Day_of_week field values ranges from 1 to 7, and the array elements are referenced from 0 to 6. Adding an empty element to the beginning of the array guarantees that the value of the Day_of_week field and the group reference match.
Find the weekend
To find out if it's Sunday or Saturday, you can write the following code:
int day = Cal.get (cal. Day_of_week);
if (day = = 6 | | | day = = 7)
{
Perform weekend-related operations
}
Days
Now we need to figure out the number of days in a month. The user enters the date (see INDEX.JSP) and sends the parameters to the processing page (refer to display.jsp). On the processing page, we set the Calendar object to the 1th day of the month.
Cal.clear ();
Cal.set (Curyear, Curmonth, 1);
int maxdays = Cal.getactualmaximum (cal. DATE);
Out.print ("〈br〉number of Days in Month:" + maxdays + "〈br〉");
We also need to know how many days in a month. The Getactualmaximum () method returns an integer value that contains the maximum number of days per month: February is 28 days, March is 31 days, and so on. In leap years, the February days return 29 days.
Once we get the maximum number of days per month, we can easily cycle through the days of the month to determine whether it is a weekend or a day of work. We use the Add () method to increment the calendar by 1, as shown in program listing A.
Show a date to a user with SimpleDateFormat
SimpleDateFormat handles the most common requirements for displaying dates, which can be used to convert dates to specific save formats. You can use the following import indicator:
〈%@ page import= "Java.text.SimpleDateFormat"%〉
The following code displays a date to the user:
SimpleDateFormat formatter = new SimpleDateFormat ("dd/mmm/yyyy");
Out.print ("〈br〉" + Formatter.format (Cal.gettime ()));
The SimpleDateFormat object accepts a string as its object constructor, which contains the display format that the user wants to take. This format string can contain additional format strings, such as spaces (""), Backslashes ("/"), and dashes ("-").
Table A lists all valid (commonly used) display formats.
Table A
Format
Example
"Dd/mmm/yyyy"
06/mar/1974
"Dd-mm-yyyy"
06-03-1974
"DD mmmmmmmmm yyyy"
Modified March 1974
"Eeeeeeeee, mmmmmmmmm dd, yyyy"
Wednesday, March 06, 1974
Valid SimpleDateFormat display format
Table B is a list of abbreviations for SimpleDateFormat parameters.
Table B
Y
Year
M
Month in the year
D
Day in month
D
Day in year
W
Week in the year
W
Week in month
E
Day in Week
SimpleDateFormat parameters
Reusing code--formattitle
There is an easy way to implement multiple format conversions on the same page: in the declaration element, you declare the Formattitle method, it accepts two parameters, one references the Calendar object, and the other returns the format.
〈%!
public string Formattitle (Calendar fcal, string format)
{
SimpleDateFormat formatter = new SimpleDateFormat (format);
Return (Formatter.format (Fcal.gettime ()));
}
%〉
To display the date, we call Formattitle () and pass the calendar and format string parameters for it.
〈%=formattitle (Cal, "dd-mmm-yyyy")%〉
Summary
After learning these simple tutorials, I believe you should now be able to manipulate and display dates with calendar and SimpleDateFormat objects.
The Add () and set () methods make it easy to configure calendar, prompting you to traverse the months and years for the business application. The Formattitle method can greatly simplify the task of displaying dates to the user, and it also simplifies the operation of converting dates into strings, which are primarily applied to data preservation of databases and XML documents.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.