zoukankan      html  css  js  c++  java
  • 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的时间提供了:

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

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

  • 相关阅读:
    php apc 安装
    apaache php 日记设计
    memcache windows64 位安装
    JavaScript的作用域与闭包
    怎样写一个简单的操作系统?
    php 关于锁的一些看法
    Windows版本Apache+php的Xhprof应用__[2]
    Windows版本Apache+php的Xhprof应用
    使用TortoiseGit对Git版本进行分支操作
    机器学习数据不均衡问题
  • 原文地址:https://www.cnblogs.com/huzi007/p/3866059.html
Copyright © 2011-2022 走看看