⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datematchtag.java

📁 Tree taglib,生成树的标签库
💻 JAVA
字号:
/*
    Copyright 2004 Jenkov Development

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/



package com.jenkov.prizetags.calendar.impl;

import javax.servlet.jsp.JspException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

public class DateMatchTag extends DateBaseTag{


    protected long      date          = 0;
    protected String    weekday       = null;
    protected long      weekdayNumber = -1;

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public String getWeekday() {
        return weekday;
    }

    public void setWeekday(String weekday) {
        this.weekday = weekday;
    }

    public int doStartTag() throws JspException {
        if(this.date > 0 && this.weekday != null){
            throw new JspException("You cannot both set weekday and date attribute of tag dateMatch");
        }

        if(matchesDate()){
            return EVAL_BODY_INCLUDE;
        }
        return SKIP_BODY;
    }

    protected boolean matchesDate() throws JspException{
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(new Date(getCurrentDate()));

        //System.out.println("day of week: " + calendar.get(Calendar.DAY_OF_WEEK));


        if(isWeekDayMatch()){
            if(weekDayMatchesCurrentDate( calendar.get(Calendar.DAY_OF_WEEK))){
                return true;
            }
            return false;
        }
        return false;
    }

    protected boolean isWeekDayMatch(){
        return getWeekday() != null;
    }

    protected boolean weekDayMatchesCurrentDate(int dayOfWeek){
        int[] weekDayNumbers = getWeekDayNumbers();

        for(int i=0; i<weekDayNumbers.length; i++){
            if(dayOfWeek == weekDayNumbers[i]) return true;
        }
        return false;
    }

    protected int[] getWeekDayNumbers(){
        StringTokenizer tokenizer = new StringTokenizer(getWeekday(), ", ");

        int[] weekDayNumbers = new int[tokenizer.countTokens()];

        int i = 0;
        while(tokenizer.hasMoreTokens()){
            String weekday = tokenizer.nextToken();
            weekDayNumbers[i] = getWeekDayNumber(weekday);
            i++;
        }

        return weekDayNumbers;
    }

    protected int getWeekDayNumber(String weekday){
        if("sunday".equals(weekday))    return 1;
        if("monday".equals(weekday))    return 2;
        if("tuesday".equals(weekday))   return 3;
        if("wednesday".equals(weekday)) return 4;
        if("thursday".equals(weekday))  return 5;
        if("friday".equals(weekday))    return 6;
        if("saturday".equals(weekday))  return 7;

        return -1;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -