java时间比较

 1     public static void main(String[] args) throws Exception{//判断某个时间是否为当前时间
 2         String dataStr="2014-07-24 12:16:04";
 3         Date createTime=new Date(System.currentTimeMillis());
 4         DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 5         Date d=dateformat.parse(dataStr);
 6         Date currentTime=new Date();
 7         long time=currentTime.getTime()-d.getTime();//当前时间与发送时间的差值 
 8         boolean flag=time<=(long)(60*60*24*1000);
 9         System.out.println("createTime"+createTime.getTime());
10         System.out.println("cureentTime"+currentTime.getTime());
11         System.out.println("d"+d.getTime());
12         System.out.println("flag"+flag);
13     }
14     

  

public static void main(String[] args){
  String date="2099-12-12";
  DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); 
  Date d1;
  try {
   d1 = dateformat.parse(date);
   Date d2=new Date();
   if(d1.before(d2)){
   System.out.println("字符串的时间早于电脑时间!");
   }
   if(d1.after(d2)){
   System.out.println("字符串的时间晚于电脑时间!");
   }
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

看了一下Date的API,发现java的时间提供了:

几个常见的方法,比较时间大小的。以前没有注意,今天又学习了。

天天进步,方便记忆理解了。

原文地址:https://www.cnblogs.com/huzi007/p/3866059.html