有没有只针对24小时的Date类,就是说无视前面的年月日 我本来想继承Date类自己写一个覆盖befor(),after()方法的,但是苦于Date类我不会设置时区 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没有只针对时分秒的date类,对时间进行操作,正如前面那位兄弟所说,用Calendar,date的大多数方法现在sun都已不推荐使用。 我自己吧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; }} 好麻烦啊,有种很简单的实现方法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;} 谢谢楼上,但是DATA类怎么设置时区啊,我之所以不用DATA,就是弄出来总有8个小时误差 JAVASE 图片按钮 跪求算法!!! java程序如何控制2台或多台打印机,这些打印机连在同一台计算机上(串口,U口) thinking in java实例解释 求skinLF的使用方法 一个小问题,在线等,送你20分 JComboBox 怎么处理键盘事件? javac对了,java还有错?怎么办? 高分求解:有关发送email的问题 Swing Jtble 排序后,出现数据刷新问题 在struts中使用command pattern 设计模式,要求controller ---xxAction是common的,公用 用代码关闭窗口
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; }
}
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;
}