When developing applications related to calendar, irregular monthly dates, the number of days per week, and the calculation of weekends have always been a featured problem. As a result, the date and time programming often makes the programmer novice causing. This article will demonstrate the usage of the date in the JSP application to provide a convenient reference for the reader to do some common tasks. The purpose of the purpose of the purpose is to handle a date parameter to handle the date parameters via the HTML form. Receive date parameters and create a Calendar object. The date of use of the Calendar object is the date of the submission is the first few days of the week and how many working days are selected in the month. Formatted the date format using the user's easy-to-read display format. The environment sample program code is tested on the Java Web server, which is configured with JDK 1.31. The example is written, and the effect running on Tomcat or other JSP web server (JDK 1.2 or later) is almost exactly the same. Passing the date to the JSP page Date parameter is selected from the 3-column drop-down list. After the user submits the form, these parameters are transmitted to the processing page. Don't forget to import java.util.calendar: <% @ page import = "java.util.calendar"%> The first task of processing page is to receive the following date parameter value: Date, Month and Year. INT CURDATE = 1; if (Request.getParameter ("curdate")! = null) {curdate = integer.parseint (Request.GetParameter ("curdate"));} Note The page parameter is converted to an int type, immediately we know Why do you have to do it. Calendar object Our goal is to create and set a Calendar object and use it for date calculation. To this end, we first need to instantiate a Calendar object. Calendar ca = calendar.getInstance (); Calendar.GetInstance () Returns a Calendar object representing the current date and time. Cal.clear (); Cal.Set (Curyear, Curmonth, Curdate); Clear () method Clears Calendar so that we may assign our own date value and prepare for future computing. Pay attention to the order of these parameters: first is the year, and finally the date. Calendar obtain information from the following is a set 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 can be methods accessible through get Calendar of (), the result is returned An integer. The following code example shows the above process. Date in one week int dayofweek = Cal.get (CAL.DAY_OF_WEEK); OUT.PRINT ("
day of week:" dayofweek "
"); Date in January int dayofmonth = Cal.get (CAL.day_of_month); Out.print ("
Day of Month:" DAYOFMONTH "
"); Positioning a specific date In order to find a specific day, you must access the DAY_OF_WEEK field. This field contains an integer value ranging from 1 to 7, 1 means Monday, 2 represents Tuesday, and the rest is pushed.
INT dayofweek = Cal.get (CAL.DAY_OF_WEEK); here is a good way to display the date to the user, that is, a declaration of an array containing the number of days in the week. Then you can easily display every date. Just use a DAY_OF_WEEK integer to access the current date within the array. String [] weekdays = new string [] {"", "monday", "tuesday", "wedness", "friday", "saturday", "sunday"}; <% = weekdays [Cal.get (CAL.day_of_week)]%> Note that the first element of the array is empty. This is because the range of DAY_OF_WEEK fields ranges from 1 to 7, and the reference range of array elements is from 0 to 6. At the beginning of an array, an empty element ensures that the value of the DAY_OF_WEEK field and the array reference are matched. Find the weekend to find out if it is right or on Sunday or Saturday, you can write the following code: int days = ca.get (CAL.DAY_OF_WEEK); if (day == 6 || day == 7) {// execution and weekend The working day now we need to calculate the number of days in a certain month. The parameter is sent to the processing page after the user enters the date (see index.jsp) (see Display.jsp). On the handling page, we set the Calendar object to the first day of the month. Cal.clear (); Cal.Set (Curyear, Curmonth, 1); int maxdays = cavermaximum (cal.date); out.print ("
Number of days in month:" maxdays "
"); We still need to know how many days in a certain month. The getActualMaximum () method returns an integer value that contains the maximum number of days per month: 2 months for 28 days, 31 days in March, etc. In the leap year, the number of days in February was returned 29 days. Once we get the number of days per month, we can easily cycle whether the number of days per month is determined whether it is a weekend or working. We increment for Calendar with the add () method, as shown by the program list A. Use SimpleDateFormat to display the user's most frequently used requirements for the date SimpleDateFormat to display the date of display, which can be used to convert the date to a specific saving format. You can use the following import indicator: <% @ page import = "java.text.SIMPLEDATEFORMAT"%> The following code is displayed to the user: SimpleDateFormat Formatter = New SimpleDateFormat ("DD / MMM / YYYYYYYY); Out.print ("
" formatter.format (cal.gettime ())); SimpleDateFormat object accepts a string as its object constructor, which contains the display format that the user wants to adopt. This format string can contain additional format strings, such as spaces (""), reverse slope ("/") and dash ("-"). Table A lists all valid (commonly used) display formats.