home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / util / Date.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  7.5 KB  |  410 lines

  1. package java.util;
  2.  
  3. public class Date {
  4.    private long value;
  5.    private boolean valueValid;
  6.    private boolean expanded;
  7.    private short tm_millis;
  8.    private byte tm_sec;
  9.    private byte tm_min;
  10.    private byte tm_hour;
  11.    private byte tm_mday;
  12.    private byte tm_mon;
  13.    private byte tm_wday;
  14.    private short tm_yday;
  15.    private int tm_year;
  16.    private int tm_isdst;
  17.    private static short[] monthOffset = new short[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  18.    private static final String[] wtb = new String[]{"am", "pm", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", "gmt", "ut", "utc", "est", "edt", "cst", "cdt", "mst", "mdt", "pst", "pdt"};
  19.    private static final int[] ttb = new int[]{0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 10000, 10000, 10000, 10300, 10240, 10360, 10300, 10420, 10360, 10480, 10420};
  20.  
  21.    public Date() {
  22.       this(System.currentTimeMillis());
  23.    }
  24.  
  25.    public Date(long date) {
  26.       this.value = date;
  27.       this.valueValid = true;
  28.       this.expanded = false;
  29.    }
  30.  
  31.    public Date(int year, int month, int date) {
  32.       this(year, month, date, 0, 0, 0);
  33.    }
  34.  
  35.    public Date(int year, int month, int date, int hrs, int min) {
  36.       this(year, month, date, hrs, min, 0);
  37.    }
  38.  
  39.    public Date(int year, int month, int date, int hrs, int min, int sec) {
  40.       this.expanded = true;
  41.       this.valueValid = false;
  42.       this.tm_millis = 0;
  43.       this.tm_sec = (byte)sec;
  44.       this.tm_min = (byte)min;
  45.       this.tm_hour = (byte)hrs;
  46.       this.tm_mday = (byte)date;
  47.       this.tm_mon = (byte)month;
  48.       this.tm_wday = 0;
  49.       this.tm_yday = 0;
  50.       this.tm_year = year;
  51.       this.computeValue();
  52.       this.expand();
  53.    }
  54.  
  55.    public Date(String s) {
  56.       this(parse(s));
  57.    }
  58.  
  59.    public static long UTC(int year, int month, int date, int hrs, int min, int sec) {
  60.       long day = (long)(date + monthOffset[month] + ((year & 3) == 0 && (year % 100 != 0 || (year + 300) % 400 == 0) && month >= 2 ? 0 : -1)) + (long)(year - 70) * 365L + (long)((year - 69) / 4) - (long)((year - 1) / 100) + (long)((year + 299) / 400);
  61.       return (long)((sec + 60 * (min + 60 * hrs)) * 1000) + 86400000L * day;
  62.    }
  63.  
  64.    public static long parse(String s) {
  65.       int year = -1;
  66.       int mon = -1;
  67.       int mday = -1;
  68.       int hour = -1;
  69.       int min = -1;
  70.       int sec = -1;
  71.       int c = -1;
  72.       int i = 0;
  73.       int n = -1;
  74.       int tzoffset = -1;
  75.       int prevc = 0;
  76.       if (s != null) {
  77.          int limit = s.length();
  78.  
  79.          while(true) {
  80.             if (i >= limit) {
  81.                if (year >= 0 && mon >= 0 && mday >= 0) {
  82.                   if (sec < 0) {
  83.                      sec = 0;
  84.                   }
  85.  
  86.                   if (min < 0) {
  87.                      min = 0;
  88.                   }
  89.  
  90.                   if (hour < 0) {
  91.                      hour = 0;
  92.                   }
  93.  
  94.                   if (tzoffset == -1) {
  95.                      return (new Date(year, mon, mday, hour, min, sec)).getTime();
  96.                   }
  97.  
  98.                   return UTC(year, mon, mday, hour, min, sec) + (long)(tzoffset * '\uea60');
  99.                }
  100.                break;
  101.             }
  102.  
  103.             c = s.charAt(i);
  104.             ++i;
  105.             if (c > 32 && c != 44 && c != 45) {
  106.                if (c == 40) {
  107.                   int depth = 1;
  108.  
  109.                   while(i < limit) {
  110.                      c = s.charAt(i);
  111.                      ++i;
  112.                      if (c == 40) {
  113.                         ++depth;
  114.                      } else if (c == 41) {
  115.                         --depth;
  116.                         if (depth <= 0) {
  117.                            break;
  118.                         }
  119.                      }
  120.                   }
  121.                } else if (c >= 48 && c <= 57) {
  122.                   for(n = c - 48; i < limit && (c = s.charAt(i)) >= 48 && c <= 57; ++i) {
  123.                      n = n * 10 + c - 48;
  124.                   }
  125.  
  126.                   if (prevc != 43 && (prevc != 45 || year < 0)) {
  127.                      if (n >= 70) {
  128.                         if (year >= 0 || c > 32 && c != 44 && c != 47 && i < limit) {
  129.                            break;
  130.                         }
  131.  
  132.                         year = n < 1900 ? n : n - 1900;
  133.                      } else if (c == 58) {
  134.                         if (hour < 0) {
  135.                            hour = (byte)n;
  136.                         } else {
  137.                            if (min >= 0) {
  138.                               break;
  139.                            }
  140.  
  141.                            min = (byte)n;
  142.                         }
  143.                      } else if (c == 47) {
  144.                         if (mon < 0) {
  145.                            mon = (byte)n - 1;
  146.                         } else {
  147.                            if (mday >= 0) {
  148.                               break;
  149.                            }
  150.  
  151.                            mday = (byte)n;
  152.                         }
  153.                      } else {
  154.                         if (i < limit && c != 44 && c > 32 && c != 45) {
  155.                            break;
  156.                         }
  157.  
  158.                         if (hour >= 0 && min < 0) {
  159.                            min = (byte)n;
  160.                         } else if (min >= 0 && sec < 0) {
  161.                            sec = (byte)n;
  162.                         } else {
  163.                            if (mday >= 0) {
  164.                               break;
  165.                            }
  166.  
  167.                            mday = (byte)n;
  168.                         }
  169.                      }
  170.                   } else {
  171.                      if (n < 24) {
  172.                         n *= 60;
  173.                      } else {
  174.                         n = n % 100 + n / 100 * 60;
  175.                      }
  176.  
  177.                      if (prevc == 43) {
  178.                         n = -n;
  179.                      }
  180.  
  181.                      if (tzoffset != 0 && tzoffset != -1) {
  182.                         break;
  183.                      }
  184.  
  185.                      tzoffset = n;
  186.                   }
  187.  
  188.                   prevc = 0;
  189.                } else if (c != 47 && c != 58 && c != 43 && c != 45) {
  190.                   int st;
  191.                   for(st = i - 1; i < limit; ++i) {
  192.                      c = s.charAt(i);
  193.                      if ((c < 65 || c > 90) && (c < 97 || c > 122)) {
  194.                         break;
  195.                      }
  196.                   }
  197.  
  198.                   if (i <= st + 1) {
  199.                      break;
  200.                   }
  201.  
  202.                   int k = wtb.length;
  203.  
  204.                   while(true) {
  205.                      --k;
  206.                      if (k < 0) {
  207.                         break;
  208.                      }
  209.  
  210.                      if (wtb[k].regionMatches(true, 0, s, st, i - st)) {
  211.                         int action = ttb[k];
  212.                         if (action != 0) {
  213.                            if (action == 1) {
  214.                               if (hour > 12 || hour < 0) {
  215.                                  throw new IllegalArgumentException();
  216.                               }
  217.  
  218.                               hour += 12;
  219.                            } else if (action <= 13) {
  220.                               if (mon >= 0) {
  221.                                  throw new IllegalArgumentException();
  222.                               }
  223.  
  224.                               mon = (byte)(action - 2);
  225.                            } else {
  226.                               tzoffset = action - 10000;
  227.                            }
  228.                         }
  229.                         break;
  230.                      }
  231.                   }
  232.  
  233.                   if (k < 0) {
  234.                      break;
  235.                   }
  236.  
  237.                   prevc = 0;
  238.                } else {
  239.                   prevc = c;
  240.                }
  241.             }
  242.          }
  243.       }
  244.  
  245.       throw new IllegalArgumentException();
  246.    }
  247.  
  248.    public int getYear() {
  249.       if (!this.expanded) {
  250.          this.expand();
  251.       }
  252.  
  253.       return this.tm_year;
  254.    }
  255.  
  256.    public void setYear(int year) {
  257.       if (!this.expanded) {
  258.          this.expand();
  259.       }
  260.  
  261.       this.tm_year = year;
  262.       this.valueValid = false;
  263.    }
  264.  
  265.    public int getMonth() {
  266.       if (!this.expanded) {
  267.          this.expand();
  268.       }
  269.  
  270.       return this.tm_mon;
  271.    }
  272.  
  273.    public void setMonth(int month) {
  274.       if (!this.expanded) {
  275.          this.expand();
  276.       }
  277.  
  278.       this.tm_mon = (byte)month;
  279.       this.valueValid = false;
  280.    }
  281.  
  282.    public int getDate() {
  283.       if (!this.expanded) {
  284.          this.expand();
  285.       }
  286.  
  287.       return this.tm_mday;
  288.    }
  289.  
  290.    public void setDate(int date) {
  291.       if (!this.expanded) {
  292.          this.expand();
  293.       }
  294.  
  295.       this.tm_mday = (byte)date;
  296.       this.valueValid = false;
  297.    }
  298.  
  299.    public int getDay() {
  300.       if (!this.expanded) {
  301.          this.expand();
  302.       } else if (this.tm_wday < 0 || !this.valueValid) {
  303.          this.computeValue();
  304.          this.expand();
  305.       }
  306.  
  307.       return this.tm_wday;
  308.    }
  309.  
  310.    public int getHours() {
  311.       if (!this.expanded) {
  312.          this.expand();
  313.       }
  314.  
  315.       return this.tm_hour;
  316.    }
  317.  
  318.    public void setHours(int hours) {
  319.       if (!this.expanded) {
  320.          this.expand();
  321.       }
  322.  
  323.       this.tm_hour = (byte)hours;
  324.       this.valueValid = false;
  325.    }
  326.  
  327.    public int getMinutes() {
  328.       if (!this.expanded) {
  329.          this.expand();
  330.       }
  331.  
  332.       return this.tm_min;
  333.    }
  334.  
  335.    public void setMinutes(int minutes) {
  336.       if (!this.expanded) {
  337.          this.expand();
  338.       }
  339.  
  340.       this.tm_min = (byte)minutes;
  341.       this.valueValid = false;
  342.    }
  343.  
  344.    public int getSeconds() {
  345.       if (!this.expanded) {
  346.          this.expand();
  347.       }
  348.  
  349.       return this.tm_sec;
  350.    }
  351.  
  352.    public void setSeconds(int seconds) {
  353.       if (!this.expanded) {
  354.          this.expand();
  355.       }
  356.  
  357.       this.tm_sec = (byte)seconds;
  358.       this.valueValid = false;
  359.    }
  360.  
  361.    public long getTime() {
  362.       if (!this.valueValid) {
  363.          this.computeValue();
  364.       }
  365.  
  366.       return this.value;
  367.    }
  368.  
  369.    public void setTime(long time) {
  370.       this.value = time;
  371.       this.valueValid = true;
  372.       this.expanded = false;
  373.    }
  374.  
  375.    public boolean before(Date when) {
  376.       return this.getTime() < when.getTime();
  377.    }
  378.  
  379.    public boolean after(Date when) {
  380.       return this.getTime() > when.getTime();
  381.    }
  382.  
  383.    public boolean equals(Object obj) {
  384.       return obj != null && obj instanceof Date && this.getTime() == ((Date)obj).getTime();
  385.    }
  386.  
  387.    public int hashCode() {
  388.       long ht = this.getTime();
  389.       return (int)ht ^ (int)(ht >> 32);
  390.    }
  391.  
  392.    public native String toString();
  393.  
  394.    public native String toLocaleString();
  395.  
  396.    public native String toGMTString();
  397.  
  398.    public int getTimezoneOffset() {
  399.       if (!this.expanded) {
  400.          this.expand();
  401.       }
  402.  
  403.       return (int)((this.getTime() - UTC(this.tm_year, this.tm_mon, this.tm_mday, this.tm_hour, this.tm_min, this.tm_sec)) / 60000L);
  404.    }
  405.  
  406.    private native void expand();
  407.  
  408.    private native void computeValue();
  409. }
  410.