A jsp (first choice for SUN Enterprise Applications) learning process for beginners (IV)
TheUnforgiven
Chapter 4 first Javabean
1. First, let's see how to get the current time and display the Code:
------------------------------------------------
<%
Java. text. SimpleDateFormat formatter = new java. text. SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Java. util. Date currentTime = new java. util. Date (); // obtain the current system time
String str_date1 = formatter. format (currentTime); // format the date and time.
String str_date2 = currentTime. toString (); // converts a Date to a String.
%>
Formatted as "yyyy-MM-dd HH: mm: ss": <% = str_date1 %>
Unformatted Date and Time in String format: <% = str_date2 %>
Unformatted Date Format: <% = currentTime %>
-------------------------------------------------
Page output content:
Formatted as "yyyy-MM-dd HH: mm: ss" Date and Time: 09:55:40
Unformatted String format Date and Time: Thu Mar 17 09:55:40 CST 2005
Unformatted Date Format: Thu Mar 17 09:55:40 CST 2005
Usually we need the formatted time: 09:55:40. Now there is a question: "09:55:40" is a string. Sometimes we need to extract information about the year, month, and day in this string. What should we do? See the following code:
-------------------- Rule string ----------------------------------------------
String of the rule (four digits of the year, two digits of the month, two digits of the day, separated by the character "-" in the middle ):
<Br> the original string is:
<%
String date = "1989-12-30 ";
Out. println (date + "<br> ");
String year = date. substring (0th); // The value ranges from 4th.
String month = date. substring (5th); // The value ranges from 7th.
String day = date. substring (8th); // you can specify 10th + 1 to digits.
Out. println ("year =" + year + ", month =" + month + ", day =" + day );
%>
----------------------------------------------------------------------------------
From the code above, we can see that this is only applicable to the rule string (4 digits in a year, 2 digits in a month, and 2 digits in a day). What if it is irregular? The year may have two or four digits, and the month and the day may have one or two digits. What should I do? By modifying the code above, you can get the following general code (this "general" has a premise that the "-" must be separated between years, months, and days ):
---------------------- Irregular string ----------------------------------------------
Irregular string (the length of year, month, and day is not necessarily separated by the character "-" in the middle ):
<Br> the original string is:
<%
String date = "04-05-6 ";
Out. println (date + "<br> ");
Int a = date. indexOf ("-"); // calculates the number of digits of the first "-".
Int B = date. lastIndexOf ("-"); // calculates the number of digits of the last "-".
Int len = date. length (); // evaluate the length of a string
Year = date. substring (0, a); // obtain the string before the first "-".
Month = date. substring (a + 1, B); // obtain the string between two "-".
Day = date. substring (B + 1, len); // obtain the string after the last "-"
Out. println ("year =" + year + ", month =" + month + ", day =" + day );
%>
----------------------------------------------------------------------------------
Now this problem has been solved. However, we will think that every time we need to split a string that represents year, month, and day, we need. writing such a piece of code on the jsp (preferred for SUN Enterprise Applications) page is not only troublesome but also confusing the page. Can you solve this problem in a clear way? Of course you can. Just Use javabean.
2. The first javabean
What is javabean? I was so upset when I came into contact with this problem, because I could not understand this concept until I wrote a few articles one after another, and I suddenly found out: Isn't it a class ?! Now, let's not care about what a class is. Let's take a look at the following javabean: