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

📄 datetime.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    {        return this.after(dt, false);    }    public boolean after(DateTime dt, boolean inclusive)    {        if (dt == null) {            return true; // arbitrary        } else        if (inclusive) {            return (this.getTimeMillis() >= dt.getTimeMillis());        } else {            return (this.getTimeMillis() > dt.getTimeMillis());        }    }    public boolean before(DateTime dt)    {        return this.before(dt, false);    }    public boolean before(DateTime dt, boolean inclusive)    {        if (dt == null) {            return false; // arbitrary        } else        if (inclusive) {            return (this.getTimeMillis() <= dt.getTimeMillis());        } else {            return (this.getTimeMillis() < dt.getTimeMillis());        }    }        public boolean equals(Object obj)     {        if (obj instanceof DateTime) {            return (this.getTimeMillis() == ((DateTime)obj).getTimeMillis());        } else {            return false;        }    }        public int compareTo(Object other)    {        if (other instanceof DateTime) {            long otherTime = ((DateTime)other).getTimeMillis();            long thisTime  = this.getTimeMillis();            if (thisTime < otherTime) { return -1; }            if (thisTime > otherTime) { return  1; }            return 0;        } else {            return -1;        }    }        // ------------------------------------------------------------------------        public interface TimeZoneProvider    {        public TimeZone getTimeZone();    }    protected TimeZone _timeZone(TimeZone tz)    {        return (tz != null)? tz : this.getTimeZone();    }        public TimeZone getTimeZone()    {        return (this.timeZone != null)? this.timeZone : DateTime.getDefaultTimeZone();    }    public String getTimeZoneID()    {        return this.getTimeZone().getID();    }    public String getTimeZoneShortName()    {        boolean dst = this.isDaylightSavings();        return this.getTimeZone().getDisplayName(dst, TimeZone.SHORT);    }        public void setTimeZone(TimeZone tz)    {        this.timeZone = tz;    }        public void setTimeZone(String tz)    {        this.setTimeZone(DateTime.getTimeZone(tz, null));    }        public static String[] readTimeZones(File tmzFile)    {        if ((tmzFile != null) && tmzFile.exists()) {            java.util.List<String> tzList = new Vector<String>();            BufferedReader br = null;            try {                br = new BufferedReader(new FileReader(tmzFile));                for (;;) {                    String rline = br.readLine();                    if (rline == null) { break; }                    String tline = rline.trim();                    if (!tline.equals("") && !tline.startsWith("#")) {                        tzList.add(tline);                    }                }                return (String[])tzList.toArray(new String[tzList.size()]);            } catch (IOException ioe) {                Print.logError("Unable to read file: " + tmzFile + " [" + ioe + "]");                return null;            } finally {                if (br != null) { try { br.close(); } catch (IOException ioe) {/*ignore*/} }            }        }        return null;    }        // ------------------------------------------------------------------------        /**    *** Returns true if the specified TimeZone is valid    *** @param tzid  The String representation of a TimeZone    *** @return True if the TimeZone is valid, false otherwise    **/    public static boolean isValidTimeZone(String tzid)    {        if ((tzid == null) || tzid.equals("")) {            return false;        } else        if (tzid.equalsIgnoreCase("GMT") ||             tzid.equalsIgnoreCase("UTC") ||             tzid.equalsIgnoreCase("Zulu")) {            return true;        } else {            // "TimeZone.getTimeZone" returns GMT for invalid timezones            TimeZone tmz = TimeZone.getTimeZone(tzid);            if (tmz.getRawOffset() != 0) { // ie. !(GMT+0)                return true; // must be a valid time-zone            } else {                return false; // GMT+0 (must be invalid)            }            // TimeZone understands more than what is listed in the available IDs            // so this check may end up failing valid time-zones.            //String tz[] = TimeZone.getAvailableIDs();            //for (int i = 0; i < tz.length; i++) {            //    if (tz[i].equals(tzid)) { return true; }            //}            //return false;        }    }    /**    *** Gets the TimeZone instance for the specified TimeZone id    *** @param tzid  The TimeZone id    *** @param dft   The default TimeZone to return if the specified TimeZone id    ***              does not exist.    *** @return The TimZone    **/    public static TimeZone getTimeZone(String tzid, TimeZone dft)    {        if ((tzid == null) || tzid.equals("")) {            return dft;        } else        if (tzid.equalsIgnoreCase("GMT") ||             tzid.equalsIgnoreCase("UTC") ||             tzid.equalsIgnoreCase("Zulu")) {            return TimeZone.getTimeZone(tzid);        } else {            // "TimeZone.getTimeZone" returns GMT for invalid timezones            TimeZone tmz = TimeZone.getTimeZone(tzid);            if (tmz.getRawOffset() != 0) { // ie. !(GMT+0)                return tmz; // must be a valid time-zone            } else {                return dft; // GMT+0 (must be invalid)            }        }    }    /**    *** Gets the TimeZone instance for the specified TimeZone id    *** @param tzid  The TimeZone id    *** @return The TimZone    **/    public static TimeZone getTimeZone(String tzid)    {        if ((tzid == null) || tzid.equals("")) {            return TimeZone.getDefault(); // local system default time-zone        } else {            // 'TimeZone' will return GMT if an invalid name is specified            return TimeZone.getTimeZone(tzid);        }    }    /**    *** Returns the default TimeZone    *** @return The default TimeZone    **/    public static TimeZone getDefaultTimeZone()    {        return DateTime.getTimeZone(null);    }    /**    *** Returns the GMT TimeZone    *** @return The GMT TimeZone    **/    public static TimeZone getGMTTimeZone()    {        return TimeZone.getTimeZone(GMT_TIMEZONE);    }    // ------------------------------------------------------------------------    private static SimpleDateFormat simpleFormatter = null;        /**    *** Returns a String representation of this DateTime instance    **/    public String toString()     {        if (simpleFormatter == null) {            // eg. "Sun Mar 26 12:38:12 PST 2006"            simpleFormatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);        }        synchronized (simpleFormatter) {            simpleFormatter.setTimeZone(this.getTimeZone());            return simpleFormatter.format(this.getDate());        }    }    // ------------------------------------------------------------------------    public String format(String fmt, TimeZone tz, StringBuffer sb)    {        SimpleDateFormat sdf = new SimpleDateFormat(fmt);        sdf.setTimeZone(this._timeZone(tz));         if (sb == null) { sb = new StringBuffer(); }        sdf.format(this.getDate(), sb, new FieldPosition(0));        return sb.toString();    }        public String format(String fmt, TimeZone tz)    {        return this.format(fmt, tz, null);    }        public String format(TimeZone tz)    {   // ie. "Oct 22, 2003 7:23:18 PM"        return this.format("MMM dd, yyyy HH:mm:ss z", tz, null);        //DateFormat dateFmt = DateFormat.getDateTimeInstance();        //dateFmt.setTimeZone(tz);        //return dateFmt.format(new java.util.Date(this.getTimeMillis()));    }        public String format(String fmt)    {        return this.format(fmt, null, null);    }    public String gmtFormat()    {        return this.gmtFormat("yyyy/MM/dd HH:mm:ss 'GMT'");    }        public String gmtFormat(String fmt)    {        return this.format(fmt, DateTime.getGMTTimeZone(), null);    }        public String gmtFormat(String fmt, StringBuffer sb)    {        return this.format(fmt, DateTime.getGMTTimeZone(), sb);    }            // ------------------------------------------------------------------------    /**    *** Returns a clone of this DateTime instance    **/    public Object clone()    {        return new DateTime(this);    }        // ------------------------------------------------------------------------        protected static String encodeHourMinuteSecond(long tod, String fmt)    {        StringBuffer sb = new StringBuffer();        int h = (int)(tod / (60L * 60L)), m = (int)((tod / 60L) % 60), s = (int)(tod % 60);        if (fmt != null) {            // format: "00:00:00", "00:00"            String f[] = StringTools.parseString(fmt, ':');            if (f.length > 0) { sb.append(StringTools.format(h,f[0])); }              // hours            if (f.length > 1) { sb.append(':').append(StringTools.format(m,f[1])); }  // minutes            if (f.length > 2) { sb.append(':').append(StringTools.format(s,f[2])); }  // seconds        } else {            sb.append(h);            sb.append(':').append(StringTools.format(m,"00"));            if (s > 0) {                sb.append(':').append(StringTools.format(s,"00"));            }        }        return sb.toString();    }    protected static int parseHourMinuteSecond(String hms)    {        return parseHourMinuteSecond(hms, 0);    }        protected static int parseHourMinuteSecond(String hms, int dft)    {        String a[] = StringTools.parseString(hms,":");        if (a.length <= 1) {            // assume all seconds            return StringTools.parseInt(hms, dft);        } else        if (a.length == 2) {            // assume hh:mm            int h = StringTools.parseInt(a[0], -1);            int m = StringTools.parseInt(a[1], -1);            return ((h >= 0) && (m >= 0))? (((h * 60) + m) * 60) : dft;        } else { // (a.length >= 3)            // assume hh:mm:ss            int h = StringTools.parseInt(a[0], -1);            int m = StringTools.parseInt(a[1], -1);            int s = StringTools.parseInt(a[2], -1);            return ((h >= 0) && (m >= 0) && (s >= 0))? ((((h * 60) + m) * 60) + s) : dft;        }    }        // ------------------------------------------------------------------------    private static String mainTMZ[] = { "US/Pacific" , "GMT", "GMT-00:00", "GMT-01:00" };        /**    *** Main entry point for testing/debugging    *** @param argv Comand-line arguments    **/    public static void main(String argv[])    {        RTConfig.setCommandLineArgs(argv);        /* All available TimeZones */        if (RTConfig.getBoolean("tzlist",false)) {            String tzid[] = (String[])ListTools.sort(TimeZone.getAvailableIDs(), null);            for (int i = 0; i < tzid.length; i++) {                TimeZone tz       = TimeZone.getTimeZone(tzid[i]);                String id         = tz.getID();                String name       = tz.getDisplayName();                String shortName  = tz.getDisplayName(false, TimeZone.SHORT);                String longName   = tz.getDisplayName(false, TimeZone.LONG);                //String testName   = TimeZone.getTimeZone(shortName).getDisplayName(false, TimeZone.SHORT);                Print.sysPrintln(tzid[i] + "[" +id + "]: " + shortName + " / " + longName); // + " [" + testName + "]");            }            System.exit(0);        }        /* Read TimeZones from file */        File tmzFile = RTConfig.getFile("tmzfile",null);        if (tmzFile != null) {            long nowTime = DateTime.getCurrentTimeSec();            String TMZ_NAME[] = DateTime.readTimeZones(tmzFile);            for (int i = 0; i < TMZ_NAME.length; i++) {                String tzname    = StringTools.leftJustify(TMZ_NAME[i], 28);                TimeZone tz      = TimeZone.getTimeZone(TMZ_NAME[i]);                String idName    = tz.getID().equals(TMZ_NAME[i])? "*" : tz.getID();                String shortName = StringTools.leftJustify(tz.getDisplayName(false, TimeZone.SHORT), 5);                String longName  = tz.getDisplayName(false, TimeZone.LONG);                int rawOfsMIN    = tz.getOffset(nowTime) / (1000 * 60);                int rawOfsHH     = Math.abs(rawOfsMIN) / 60;                int rawOfsMM     = Math.abs(rawOfsMIN) % 60;                String gmtStr    = "GMT"+((rawOfsMIN>=0)?"+":"-")+StringTools.format(rawOfsHH,"00")+":"+StringTools.format(rawOfsMM,"00");                Print.sysPrintln(tzname+" ["+gmtStr+"]: " + idName + " , " + shortName + " , " + longName);            }            System.exit(0);        }        /* TimeZone */        String tmz = RTConfig.getString("tmz",null);        if ((tmz != null) && !tmz.equals("")) {            long nowTime = DateTime.getCurrentTimeSec();            TimeZone tz = TimeZone.getTimeZone(tmz);            int rawOfsMIN  = (tz.getOffset(nowTime) + tz.getDSTSavings()) / (1000 * 60);            int rawOfsHH   = Math.abs(rawOfsMIN) / 60;            int rawOfsMM   = Math.abs(rawOfsMIN) % 60;            String gmtStr  = "GMT"+((rawOfsMIN>=0)?"+":"-")+StringTools.format(rawOfsHH,"00")+":"+StringTools.format(rawOfsMM,"00");            Print.sysPrintln(tmz + " [" + gmtStr + "]: " + tz);            Print.sysPrintln("Time : " + (new DateTime(nowTime,tz)));            Print.sysPrintln("GMT  : " + (new DateTime(nowTime,DateTime.getGMTTimeZone())));            System.exit(0);        }                /* time zones */        String tzStr   = RTConfig.getString("tz",null);        TimeZone tz    = (tzStr != null)? DateTime.getTimeZone(tzStr) : DateTime.getDefaultTimeZone();        TimeZone pstTZ = DateTime.getTimeZone("US/Pacific");        TimeZone gmtTZ = DateTime.getGMTTimeZone();        /* time */        long now = RTConfig.getLong("time",-1L);        if (now <= 0L) {            String stime = RTConfig.getString("stime","");            if (!stime.equals("")) {                DateTime dtTime = new DateTime();                try {                    dtTime.setDate(stime,pstTZ);                    Print.sysPrintln("Parsed '" + stime + "' ==> " + dtTime);                    now = dtTime.getTimeSec();                } catch (ParseException pde) {                    Print.sysPrintln("Unable to parse date: " + stime);                }            }            if (now <= 0L) {                now = DateTime.getCurrentTimeSec();            }        }        /* display dates */        DateTime gmtNowDt = new DateTime(now,gmtTZ);        DateTime pstNowDt = new DateTime(now,pstTZ);        DateTime pstStrDt = new DateTime(pstNowDt.getDayStart(),pstTZ);        DateTime pstEndDt = new DateTime(pstNowDt.getDayEnd(),pstTZ);        DateTime tmzNowDt = new DateTime(now,tz);        Print.sysPrintln("");        Print.sysPrintln("GMT time  : " + gmtNowDt + " [" + now + ":0x" + StringTools.toHexString(now,32) + "]");        Print.sysPrintln("TZ  time  : " + tmzNowDt);        Print.sysPrintln("PST time  : " + pstNowDt);        Print.sysPrintln("PST start : " + pstStrDt + " [" + pstStrDt.getTimeSec() + "]");        Print.sysPrintln("PST end   : " + pstEndDt + " [" + pstEndDt.getTimeSec() + "]");    }    }

⌨️ 快捷键说明

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