StringUtil.java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class StringUtil {
public StringUtil(){}
private String dateStr;//日期字符串
private Calendar calendar=null;
public String getDateStr() {
return dateStr;
}
public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}
public Calendar getCalendar() {
Date date=null;
SimpleDateFormat format=null;//声明格式化日期的对象
if(dateStr!=null){
format = new SimpleDateFormat("yyyy-MM-dd");//创建日期的格式化类型
calendar = Calendar.getInstance();
try {
date=format.parse(dateStr);
calendar.setTime(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return calendar;
}
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
}
}
index.jsp
<body>
<form action="result.jsp" method="post">
<table>
<tr>
<td align="right">请输入日期字符串:</td>
<td><input type="text" name="datestr" /><font>格式为2018-8-19</font></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="转换"/></td>
</tr>
</table>
</form>
</body>
result.jsp
<body>
<%
request.setCharacterEncoding("utf-8");
String dateStr= request.getParameter("datestr");
%>
<jsp:useBean id="strBean" class="com.count.StringUtil"></jsp:useBean>
<jsp:setProperty property="dateStr" name="strBean" value="<%=dateStr %>"/>
<table>
<tr>
<td>输入的日期是:</td>
<td align="left">
<jsp:getProperty property="dateStr" name="strBean"/>
</td>
</tr>
<tr>
<td align="right" width="100">转换后的Calendar对象中的日期值:</td>
<td>
<%
Calendar calendar = strBean.getCalendar();
%>
年:<%=calendar.get(calendar.YEAR) %><br>
月:<%=calendar.get(calendar.MONTH)+1 %><br>
日:<%=calendar.get(calendar.DAY_OF_MONTH) %>
</td>
</tr>
</table>
</body>
11.PNG
12.PNG
网友评论