Usage of dates in JSP

Source: Internet
Author: User
Tags days in month html form string format valid java web

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.








Purpose










The purpose of the
sample application is to:





an HTML form to submit a date parameter to a JSP page for processing.


receives date parameters and creates a calendar object.


uses the Calendar object to find the date of the week and how many working days are in the selected month.


formats dates in a display format that is easy for users to read.





Environmental




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 the date to the JSP page through the form




The
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 during calendar programming:


〈%@ page import= "Java.util.Calendar"%〉




The 1th task of the
processing 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 the page parameters are converted to type int, and we'll know 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





These fields are accessible through the calendar's Get () method, and the result returns an integer. The following code example shows the above procedure.





a week's date


int dayofweek = Cal.get (cal. Day_of_week);


Out.print ("〈br〉day of Week:" + DayOfWeek + "〈br〉");





date in January


int dayofmonth = Cal.get (cal. Day_of_month);


Out.print ("〈br〉day of Month:" + dayofmonth + "〈br〉");





to locate a specific date











must access the Day_of_week field in order to find a specific day of the week. 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


}











working day





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.











Displays the date with SimpleDateFormat to the user





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 to show the user the date:


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


Sample





"Dd/mmm/yyyy"


06/mar/1974





"Dd-mm-yyyy"


06-03-1974





"dd mmmmmmmmm yyyy"


March 1974





"Eeeeeeeee, mmmmmmmmm dd, yyyy"


Wednesday, March 06, 1974








Valid SimpleDateFormat display format











Table B is an abbreviated list of SimpleDateFormat parameters.





Table B





y


year





M


Month in the year





D


Day in month





D


Day in the year





W


Week in the year





W


Week in month





E


Day in Week








SimpleDateFormat parameter








Reuse Code--formattitle





there is a simple way to implement multiple format conversions on the same page: in the declaration element you declare the Formattitle method, it accepts two arguments, 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





learned the simple tutorials above, I believe you should now be able to use calendar and SimpleDateFormat objects to manipulate and display the date.




The
Add () and set () methods make it easy to configure calendar, prompting you to traverse the months and years for a 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.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.