我本来想继承Date类自己写一个覆盖befor(),after()方法的,但是苦于Date类我不会设置时区

解决方案 »

  1.   

    没有只针对时分秒的date类,对时间进行操作,正如前面那位兄弟所说,用Calendar,date的大多数方法现在sun都已不推荐使用。
      

  2.   

    我自己吧GregorianCalendar继承了,终于做到了,我把代码贴上来吧package Time;import java.util.GregorianCalendar;
    import java.util.TimeZone;public class MyTimeClass extends GregorianCalendar
    { public MyTimeClass (TimeZone tz)
    {
    super(tz);
    }
    public boolean after(Object arg0)
    {
    // TODO Auto-generated method stub
    if (!(arg0 instanceof GregorianCalendar))
    super.after(arg0);
    else
    {
    GregorianCalendar compare = (GregorianCalendar) arg0;
    if (this.get(GregorianCalendar.HOUR_OF_DAY) > compare
    .get(GregorianCalendar.HOUR_OF_DAY))
    return true;
    else if (this.get(GregorianCalendar.HOUR_OF_DAY) < compare
    .get(GregorianCalendar.HOUR_OF_DAY))
    return false;
    if (this.get(GregorianCalendar.MINUTE) > compare
    .get(GregorianCalendar.MINUTE))
    return true;
    else if (this.get(GregorianCalendar.MINUTE) < compare
    .get(GregorianCalendar.MINUTE))
    return false;
    if (this.get(GregorianCalendar.SECOND) > compare
    .get(GregorianCalendar.SECOND))
    return true;
    else if (this.get(GregorianCalendar.SECOND) < compare
    .get(GregorianCalendar.SECOND))
    return false;
    if (this.get(GregorianCalendar.MILLISECOND) > compare
    .get(GregorianCalendar.MILLISECOND))
    return true;
    else if (this.get(GregorianCalendar.MILLISECOND) < compare
    .get(GregorianCalendar.MILLISECOND))
    return false;
    return false; }
    return areFieldsSet; } @Override
    public boolean before(Object arg0)
    {
    if (!(arg0 instanceof GregorianCalendar))
    super.before(arg0);
    else
    {
    GregorianCalendar compare = (GregorianCalendar) arg0;
    if (this.get(GregorianCalendar.HOUR_OF_DAY) < compare
    .get(GregorianCalendar.HOUR_OF_DAY))
    return true;
    else if (this.get(GregorianCalendar.HOUR_OF_DAY) > compare
    .get(GregorianCalendar.HOUR_OF_DAY))
    return false;
    if (this.get(GregorianCalendar.MINUTE) < compare
    .get(GregorianCalendar.MINUTE))
    return true;
    else if (this.get(GregorianCalendar.MINUTE) > compare
    .get(GregorianCalendar.MINUTE))
    return false;
    if (this.get(GregorianCalendar.SECOND) < compare
    .get(GregorianCalendar.SECOND))
    return true;
    else if (this.get(GregorianCalendar.SECOND) > compare
    .get(GregorianCalendar.SECOND))
    return false;
    if (this.get(GregorianCalendar.MILLISECOND) < compare
    .get(GregorianCalendar.MILLISECOND))
    return true;
    else if (this.get(GregorianCalendar.MILLISECOND) > compare
    .get(GregorianCalendar.MILLISECOND))
    return false;
    return false;
    }
    return areFieldsSet; }
    }
      

  3.   

    好麻烦啊,有种很简单的实现方法
    DateFormat df=new SimpleDateFormat("HH:mm:ss");
    Date d1=new Date();
    Date d2=new Date();
    String s1=df.format(d1);
    String s2=df.format(d2);if(s1.compareTo(s2)>0)
    {
      //s1>s2;
    }
      

  4.   

    谢谢楼上,但是DATA类怎么设置时区啊,我之所以不用DATA,就是弄出来总有8个小时误差