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

📄 datearithmetic.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 JAVA
字号:
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.lib.date;


import java.util.Calendar;

import org.mandarax.kernel.Function;
import org.mandarax.kernel.Predicate;

/**
 * Class serving predicates and functions needed for date manipulations.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.4
 */
public class DateArithmetic {

    // constants
    public static final String[] MONTHS = {
        "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
    };
    public static final String[] DAYS = {
    	// indexing starts with 1 !!!
        null,"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday"
    };

    // class representing the diff function - returns the difference of two calendars in milli seconds as int value
    public static class Diff extends BinaryDateFunction1 {

        public String getName() {
            return "diff";
        }
        ;

        public int compute(Calendar c1, Calendar c2) {
        	Long l = new Long(c1.getTimeInMillis()+c1.get(Calendar.ZONE_OFFSET)-c2.getTimeInMillis()+c2.get(Calendar.ZONE_OFFSET));        	
            return l.intValue(); // return difference in milli seconds as int
        }
        ;
    };
    
    
    // returns the day of the week (starting with sunday = 0)
    public static class Day extends UnaryDateFunction1 {

        public String getName() {
            return "day of month";
        }
        ;

        public int compute(Calendar c) {
            return c.get (Calendar.DAY_OF_MONTH);
        }
        ;
    }
    ;

    // returns the day of the week by its (english) name (sunday, monday)
    public static class DayByName extends UnaryDateFunction2 {

        public String getName() {
            return "day by name";
        }

        public String compute(Calendar c) {
            return DAYS[c.get (Calendar.DAY_OF_WEEK)];
        }
    }
    ;

    // returns the hours (0..23)
    public static class Hours extends UnaryDateFunction1 {

        public String getName() {
            return "hour";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.HOUR);
        }
    }
    ;

    // returns the minutes (0..59)
    public static class Minutes extends UnaryDateFunction1 {

        public String getName() {
            return "minute";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.MINUTE);
        }
    }

    // returns the month (0..11)
    public static class Month extends UnaryDateFunction1 {

        public String getName() {
            return "month";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.MONTH);
        }
    }

    // returns the month by its (english) name (january, february, ..)
    public static class MonthByName extends UnaryDateFunction2 {

        public String getName() {
            return "month by name";
        }

        public String compute(Calendar c) {
            return MONTHS[c.get (Calendar.MONTH)];
        }
    }
    ;

    // returns the seconds (0..59)
    public static class Seconds extends UnaryDateFunction1 {

        public String getName() {
            return "second";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.SECOND);
        }
    }
    ;

    // returns the year (0..11)
    public static class Year extends UnaryDateFunction1 {

        public String getName() {
            return "year";
        }

        public int compute(Calendar c) {
            return c.get (Calendar.YEAR);
        }
    }
    ;

    // compares dates (>)  
    public static class After extends BinaryDatePredicate {

        public String getName() {
            return "after";
        }

        public boolean compute(Calendar c1, Calendar c2) {
        	return c1.after (c2);
        }
    }
    ;

    // compares dates (>=)
    // added by Adrian 
    public static class After_Or_Equals extends BinaryDatePredicate {

        public String getName() {
            return "after_or_equals";
        }

        public boolean compute(Calendar c1, Calendar c2) {
        	if (c1.equals(c2)) return true;
        	else if (c1.after(c2)) return true;
        	else return false;
        	
        }
    }
    ;    
    
    // compares dates (<)
    public static class Before extends BinaryDatePredicate {

        public String getName() {
            return "before";
        }

        public boolean compute(Calendar c1, Calendar c2) {
            return c1.before (c2);
        }
    }
    ;

    // compares dates (<=) 
    // added by Adrian
    public static class Before_Or_Equals extends BinaryDatePredicate {

        public String getName() {
            return "before_or_equals";
        }

        public boolean compute(Calendar c1, Calendar c2) {
        	if (c1.equals(c2)) return true;
        	else if (c1.before(c2)) return true;
        	else return false;
        }
    }
    ;    
    
    // compares dates
    public static class Equals extends BinaryDatePredicate {

        public String getName() {
            return "equals (dates)";
        }

        public boolean compute(Calendar c1, Calendar c2) {
        	return c1.equals (c2);
        }
    }
    ;
    

    // unary functions
    public static final Function DAY         = new DateArithmetic.Day ();
    public static final Function DAY_BY_NAME =
        new DateArithmetic.DayByName ();
    public static final Function HOURS         = new DateArithmetic.Hours ();
    public static final Function MINUTES       =
        new DateArithmetic.Minutes ();
    public static final Function MONTH         = new DateArithmetic.Month ();
    public static final Function MONTH_BY_NAME =
        new DateArithmetic.MonthByName ();
    public static final Function SECONDS = new DateArithmetic.Seconds ();
    public static final Function YEAR    = new DateArithmetic.Year ();

    // binary functions    
    public static final Function DIFF        = new DateArithmetic.Diff ();
    
    // binary predicates
    public static final Predicate AFTER  = new DateArithmetic.After ();
    public static final Predicate AFTER_OR_EQUALS  = new DateArithmetic.After_Or_Equals ();
    public static final Predicate BEFORE = new DateArithmetic.Before ();
    public static final Predicate BEFORE_OR_EQUALS = new DateArithmetic.Before_Or_Equals ();
    public static final Predicate EQUALS = new DateArithmetic.Equals ();

    
    // all functions and predicates
    public static final Function[]  ALL_FUNCTIONS  = {
        DIFF, YEAR, MONTH, MONTH_BY_NAME, DAY, DAY_BY_NAME, HOURS, MINUTES, SECONDS
    };
    public static final Predicate[] ALL_PREDICATES = { EQUALS, BEFORE, AFTER, BEFORE_OR_EQUALS, AFTER_OR_EQUALS};
    
}

⌨️ 快捷键说明

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