hpdatetime.java

来自「一个简单的visio程序。」· Java 代码 · 共 2,210 行 · 第 1/5 页

JAVA
2,210
字号
/*----------------------
FUNCTION:
        DATE[$],        TIME[$],         NOW(),
        YEAR(date),     Month(date),     DAY(date),
        HOUR(date),     MINUTE(date),    SECOND(date),
        WEEKDAY(date[,firstdayofweek]),  TIMER(),
        TIMESERIAL(hour,minute,second),  DATESERIAL(year,month,day),
        DATEVALUE(date),                 TIMEVALUE(date),
        DATEADD(interval,number,date),   DATEPART(interval,date[,firstdayofweek [,firstdayofyear]]),
        DATEDIFF(interval,date1, date2[, firstdayofweek[,firstweekofyear]]),
        ISDATE( date )
total   19
Author:Hu xia
-----------------------*/
package HPCore.stdfunc;
import  HPCore.Exception.*;
import  HECore.stddata.*;
//import HcBean.*;
import java.util.*;

public class hpdatetime
{

    /*-------Statement: Date------*/
    public static void DATE( Variant datevalue) throws HpException
    {
        double  dbl=0 ; // = getdt_db( datevalue );
        String  str=new String("");
        boolean noerr=true;

        int type = datevalue.getType();
        if( type!=Variant.V_STR || type!=Variant.V_FIX_STR )
            throw new HpException(13, "Type mismatch");
        else
        {
            if( type==Variant.V_STR )
            {
                str = datevalue.strValue();
            }
            else
            {
                fixedString  fixstr = ((VString)datevalue).getfixedLenStrValue();
                str = fixstr.strValue();
            }
            try
            {
                dbl = (new Double(str)).doubleValue();
                if( str.indexOf('.')!=-1 && dbl>0 )
                {
                   String tmpstr = str.substring(str.indexOf('.')+1 );
                   double tmpdd = (new Double(tmpstr)).doubleValue();
                   if( tmpdd<60 )
                       str = str.replace('.', ':');
                }
                else
                    noerr = false;
            }catch( Exception err)
            {
                if( noerr==false )
                    throw new HpException( 13,"Type mismatch");

                UdtDatetime  udt=new UdtDatetime();
                int[] standdt = udt.DtFormat(str);
                if( standdt==null)
                    throw new HpException(13,"Type mismatch");
                dbl = udt.StrToDbl(standdt);
            }
        }

    	HCDateTimeStruct tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        
        Date today = new Date();
        today.setYear(tm.dt_year);
        today.setMonth(tm.dt_month);
        today.setDate(tm.dt_mday);
     }


    /*-------Statement: Time------*/
    public static void TIME( Variant timevalue) throws HpException
    {
        if(  ISDATE(timevalue)==false )
            throw new HpException(13,"Type mismatch");

        double dbl = getdt_db( timevalue );
    	HCDateTimeStruct tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        
        Date  today = new Date();
        today.setHours(tm.dt_hour);
        today.setMinutes(tm.dt_minute);
        today.setSeconds(tm.dt_second);
    }

    /**
	 *Date[$]
	 *Returns the current system date.Date$ return a string,Date return a exp string
	 */
    public static String DATE$() throws HpException
    {
    Date  today = new Date();
    int  month = today. getMonth() + 1;
    int  m_day = today.getDate();
    int  year = today.getYear();
   
    String  sMonth  =  month<10 ? ("0" + month) : "" + month;
    String  sDate   =  m_day <10 ? ("0" + m_day ) : "" + m_day;
    String m_VBDate = sMonth  + "-" + sDate + "-" + year;
    return m_VBDate;
    }

    /*-------Function: Date------*/
    public static HDate DATE() throws HpException
    {
    UdtDatetime     Datetime = new UdtDatetime();
    double          d=0d;

    Date  today = new Date();
    int[] m_VBDate = {  today.getMonth()+1,
                        today.getDate(),
                        today.getYear(),
                        0,0,0
                     };
    
    GGRESULT        ddate = Datetime.dtcommon(m_VBDate);
    d = ddate.valued;		 /* to rejudd=st it */

    return new HDate(d);
    }


    /**
	 *Time[$]
	 *Returns a Date indicating the current system time.
	 */
    public static String TIME$() throws HpException
    {
    int   hour, minute, second;
    
    Date today = new Date();
    hour = today.getHours();
    minute = today.getMinutes();
    second = today.getSeconds();
    String  s_hour   = hour<10 ? "0" + hour : Integer.toString(hour);
    String  s_minute = minute<10  ? "0"+ minute : Integer.toString(minute);
    String  s_second = second<10 ? "0"+ second : Integer.toString(second);

    String vb_Time = s_hour + ":" + s_minute + ":" + s_second;
    return vb_Time;
    }

    /*-------- Function: Time()--------*/
    public static HDate TIME() throws HpException
    {
    double   d=0;
    int      hour, am_pm;

    Date   today = new Date();
    hour = today.getHours();
    int[]   vb_Time = {     0,0,0,
                            hour,
                            today.getMinutes(),
                            today.getSeconds()
                      };
    UdtDatetime  Datetime=new UdtDatetime();
    GGRESULT ddate = Datetime.dtcommon(vb_Time);
    d = ddate.valuet;
    return new HDate(d);
    }

    /**
	 *Now
	 *Returns the current date and time according to the setting of your computer's
	 *system date and time.
	 */
    public static HDate NOW() throws HpException
    {
    int        am_pm;

    Date today = new Date();
    int[]   vb_Now = {      today.getMonth()+1,
                            today.getDate(),
                            today.getYear(),
                            today.getHours(),
                            today.getMinutes(),
                            today.getSeconds()
                     };

    UdtDatetime  Datetime=new UdtDatetime();
    GGRESULT ddate = Datetime.dtcommon(vb_Now);

    if( ddate.valued<0 )
        ddate.valuet = - ddate.valuet;
    double      dt = ddate.valued + ddate.valuet;
    return new HDate(dt);
    }

    /**
	 *get datetime double value from exp
	 */
    static double getdt_db(Variant exp) throws HpException
    {
    double   dbl=0;
    switch (exp.getType())
	{
    case Variant.V_NULL:
        throw new HpException(94,"Invalid use of Null");

    case Variant.V_FIX_STR:
        fixedString fixstr = ((VString)exp).getfixedLenStrValue();
        String  str = fixstr.strValue();
		try{
            dbl = (new Double(str)).doubleValue( );
            if( str.indexOf('.')!=-1 && dbl>0 )
            {
               String tmpstr = str.substring(str.indexOf('.')+1 );
               double tmpdd = (new Double(tmpstr)).doubleValue();
               if( tmpdd<60 )
                   str = str.replace('.', ':');
            }
        }catch(  Exception e){;}

        try
        {
            dbl = hpstring.strtodbl(str);

        }catch( HpException err)
        {
            UdtDatetime  udt=new UdtDatetime();
            int[] standdt = udt.DtFormat(str);
            if( standdt==null)
                throw new HpException(13,"Type mismatch");
            dbl = udt.StrToDbl(standdt);
        }
    break;

    case Variant.V_STR:
    String   s = exp.strValue();
    try{
        dbl = (new Double(s)).doubleValue( );

        if( s.indexOf('.')!=-1 && dbl>0 )
        {
           String tmp = s.substring(s.indexOf('.')+1 );
           double tmpd = (new Double(tmp)).doubleValue();
           double tmpy = (new Double(s.substring(0,s.indexOf('.'))).doubleValue() );

           if(tmpy<24 && tmpd<60 )
               s = s.replace('.', ':');
        }
    }catch(  Exception e){;}

    try
    {
    dbl = hpstring.strtodbl(s);
    }catch( HpException err)
    {
        UdtDatetime  udt=new UdtDatetime();
        int[] standdt = udt.DtFormat(s);
        if( standdt==null)
            throw new HpException(13,"Type mismatch");
        dbl = udt.StrToDbl(standdt);
    }
    break;

    case Variant.V_EMPTY:
    case Variant.V_VAR:
    case Variant.V_BYTE:
    case Variant.V_BOL:
    case Variant.V_INT:
    case Variant.V_LONG:
    case Variant.V_DATE:
    case Variant.V_CURR:
    case Variant.V_DBL:
    case Variant.V_SINGLE:
    dbl = exp.dblValue();
    break;

    case Variant.V_OBJ:
    //Wrapper  obj = exp.getObject();
    //if( obj==null)
    //    throw new HpException( 91, "Object variable or with block variable not set");
    dbl = ((VObject)exp).getDefaultProperty().dblValue();
    break;

    default:
        throw new HpException(13, "Type mismatch");
    }
    if(dbl < -657435 || dbl > 2958465)
        throw new HpException(95,"Invalid procedure call");
    return dbl;
    }

    /**
	 *year(date)
	 *Returns a whole number representing the year.if date is null,then return null.
	 */
    public static Variant YEAR(Variant  exp) throws HpException
    {
   	HCDateTimeStruct	    tm;
    Variant                  v = null;

    int  type = exp.getType();
    if( type==Variant.V_NULL)
    {
        v = new VNull();
    }
    else
    {
    	double  dbl = getdt_db( exp);
        tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        v = new VInt((short)tm.dt_year );
    }
    return  v;
    }

    public static short YEAR(boolean  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
       	double  dbl = exp==true ? (double)-1:(double)0;
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_year);
    }
    public static short YEAR(HByte  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)(exp.byteValue() ));
        return (short)(tm.dt_year);
    }
    public static short YEAR(short  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_year);
    }
    public static short YEAR(int  exp) throws HpException
    {
    	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_year);
   }
    public static short YEAR(float  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_year);
    }
    public static short YEAR(double  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(exp);
        return (short)(tm.dt_year);
    }
    public static short YEAR(HCurr  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
       	double  dbl = exp.dblValue();
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_year);
    }
    public static short YEAR(HDate  exp) throws HpException
    {
        double dbl = exp.dblValue();
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_year);
    }
    public static short YEAR(fixedString  exp) throws HpException
    {
        double dbl = getdt_db(new VString( exp.strValue() ) );
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_year);
    }
    public static short YEAR(String  exp) throws HpException
    {
        double dbl = getdt_db( (Variant)(new VString(exp) ));
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_year);
    }

    /**
	 *Month(date)
	 *Returns a whole number between 1 and 12, inclusive, representing the month of
	 *the year.if date contain null then return null.
	 */
    public static Variant MONTH(Variant  exp) throws HpException
    {
	HCDateTimeStruct	    tm;
    Variant                  v = null;

    int  type = exp.getType();
    if( type==Variant.V_NULL)
    {
        v = new VNull();
    }
    else
    {
        double  dbl = getdt_db( exp);
        tm = new HCDateTimeStruct();
        tm.DT_DblToStruc(dbl);
        v = new VInt( (short)tm.dt_month );
    }
    return v;
    }

    public static short MONTH(boolean  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
       	double  dbl = exp==true ? (double)-1:(double)0;
        tm.DT_DblToStruc(dbl);
        return (short)(tm.dt_month);
    }
    public static short MONTH(HByte  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)(exp.byteValue()));
        return (short)(tm.dt_month);
    }
    public static short MONTH(short  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_month);
    }
    public static short MONTH(int  exp) throws HpException
    {
    	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_month);
   }
    public static short MONTH(float  exp) throws HpException
    {
       	HCDateTimeStruct	    tm = new HCDateTimeStruct();
        tm.DT_DblToStruc((double)exp);
        return (short)(tm.dt_month);
    }
    public static short MONTH(double  exp) throws HpException
    {

⌨️ 快捷键说明

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