判断输入日期是否为当前日期
2018-08-18 本文已影响0人
神坛下的我
StringUtil.java
import java.util.Calendar;
public class StringUtil {
private String dateStr;//用户输入的日期
private boolean today;//判断是否为今天
private String cue;//提示信息
public StringUtil(){}
public String getDateStr() {
return dateStr;
}
public void setDateStr(String dateStr) {
dateStr=dateStr.replaceAll(" ", "");//替换日期中的空格为""
this.dateStr = dateStr;
}
public boolean isToday() {
String dateArr[]=dateStr.split("-");//将日期字符串分解成数组
int year=Integer.parseInt(dateArr[0]);
int month=Integer.parseInt(dateArr[1]);
int date=Integer.parseInt(dateArr[2]);
Calendar now=Calendar.getInstance();
int nowYear=now.get(now.YEAR);
int nowMonth=now.get(now.MONTH)+1;
int nowDate=now.get(now.DAY_OF_MONTH);
if(year==nowYear&&month==nowMonth&&date==nowDate){
cue="输入的日期为当前日期!";
today=true;
}
else {
cue="输入的日期不是当前日期!";
today=false;
}
return today;
}
public void setToday(boolean today) {
this.today = today;
}
public String getCue() {
return cue;
}
public void setCue(String cue) {
this.cue = cue;
}
}
index.jsp
<body>
<form action="result.jsp" method="post">
<table>
<tr>
<td align="right">请输入日期:</td>
<td><input type="text" name="datestr" /><font>格式为:2012-12-12</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>是否为当前日期:</td>
<td align="center">
<jsp:getProperty property="today" name="strBean"/>
</td>
</tr>
<tr>
<td>提示信息:</td>
<td align="right">
<jsp:getProperty property="cue" name="strBean"/>
</td>
</tr>
</table>
</body>
6.PNG
7.PNG