将Calendar对象转换为日期字符串
2018-08-19 本文已影响0人
神坛下的我
StringUtil.java
import java.util.Calendar;
public class StringUtil {
public StringUtil(){}
private String dateStr=null;//日期字符串
private Calendar calendar=null;
public String getDateStr() {
int year = calendar.get(calendar.YEAR);
int month = calendar.get(calendar.MONTH)+1;
int date = calendar.get(calendar.DAY_OF_MONTH);
dateStr = year+"-"+month+"-"+date;
return dateStr;
}
public void setDateStr(String dateStr) {
this.dateStr = dateStr;
}
public Calendar getCalendar() {
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");//获得表单中的字符串
String date[] = dateStr.split("-");
int y = Integer.parseInt(date[0]);
int m = Integer.parseInt(date[1]);
int d = Integer.parseInt(date[2]);
Calendar c= Calendar.getInstance();
c.set(y,m-1, d);
%>
<jsp:useBean id="strBean" class="com.count.StringUtil"></jsp:useBean>
<jsp:setProperty property="calendar" name="strBean" value="<%=c %>"/>
<table>
<tr>
<td align="right" width="100">转换为日期字符串:</td>
<td>
<jsp:getProperty property="dateStr" name="strBean"/>
</td>
</tr>
</table>
</body>
13.PNG