zoukankan      html  css  js  c++  java
  • 小TIP:Java里的时间比较

    import java.text.*;
    import java.util.*;

    public class TimeCompare{

     public static void main(String[] args){
      boolean flag = isDateBefore("2004-09-09 12:12:12","2005-09-09 16:00:00");
      System.out.println(flag);
      flag = isDateBefore("2006-09-09 01:01:01","2005-09-09 16:00:00");
      System.out.println(flag);
      flag = isDateBefore("2005-09-09 01:01:01");
      System.out.println(flag);
     }

     //判断时间date1是否在时间date2之前
     //时间格式 2005-4-21 16:16:34
     public static boolean isDateBefore(String date1,String date2){
      try{
       DateFormat df = DateFormat.getDateTimeInstance();
       return df.parse(date1).before(df.parse(date2));
      }catch(ParseException e){
       System.out.print("[SYS] " + e.getMessage());
       return false;
      }
     }
     
     //判断当前时间是否在时间date2之前
     //时间格式 2005-4-21 16:16:34
     public static boolean isDateBefore(String date2){
      try{
       Date date1 = new Date();
       DateFormat df = DateFormat.getDateTimeInstance();
       return date1.before(df.parse(date2));
      }catch(ParseException e){
       System.out.print("[SYS] " + e.getMessage());
       return false;
      }
     }

    }

  • 相关阅读:
    linux输出信息调试信息重定向
    JDBC复习
    在Java中用for循环打印菱形
    深入.NET框架
    C#中等号左右的文本值交换
    使用集合组织相关数据
    深入类的方法
    值传递和引用传递专题案例
    深入C#数据类型
    Computer Vision Rescources
  • 原文地址:https://www.cnblogs.com/longware/p/13382404.html
Copyright © 2011-2022 走看看