判断输入日期是否为当前日期

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


    
请输入日期: 格式为:2012-12-12

result.jsp


    <%
        request.setCharacterEncoding("utf-8");
        String datestr= request.getParameter("datestr");
    %>
    
    
    
    
输入的日期:
是否为当前日期:
提示信息:
6.PNG
7.PNG

你可能感兴趣的:(判断输入日期是否为当前日期)