目录

都是常用的日期之间的比较方法,供以后参考。

热身:获取当前时间

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String nowDate = df.format(new Date());// new Date()为获取当前系统时间

我一直以为date类型不如string类型随心所欲 常用日期之间比较的工具类 ,所以下面的比较都是string类型的date进行比较。如果你真的真的就是那么倔强,ok,下面是date转string的方法:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date = df.format(Date类型的时间);

下面是正题:

    /**
     * 获取昨日的日期字符串
     * @return
     */
    public static String GetYesterDay(){
        //获取昨日时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        Log.i("ListFragment - Date:",yesterday);
        return yesterday;
    }

    /**
     * 获得指定日期的前一天
     * @param specifiedDay
     * @return
     * @throws Exception
     */
    public static String getSpecifiedDayBefore(String specifiedDay){
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day-1);

        String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        Log.i("前一天getSpecifiedDayBefore",dayBefore);
        return dayBefore;
    }
    /**
     * 获得指定日期的后一天
     * @param specifiedDay
     * @return
     */
    public static String getSpecifiedDayAfter(String specifiedDay){
        Calendar c = Calendar.getInstance();
        Date date=null;
        try {
            date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(date);
        int day=c.get(Calendar.DATE);
        c.set(Calendar.DATE,day+1);

        String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        Log.i("后一天getSpecifiedDayBefore",dayAfter);
        return dayAfter;
    }

    /**
     *
     * @param smdate
     * @param bdate
     * @return 两个string类型日期之间相差的天数
     */
    public static int daysBetween(String smdate,String bdate){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        long time1 = 0;
        long time2 = 0;

        try{
            cal.setTime(sdf.parse(smdate));
            time1 = cal.getTimeInMillis();
            cal.setTime(sdf.parse(bdate));
            time2 = cal.getTimeInMillis();
        }catch(Exception e){
            e.printStackTrace();
        }
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     *
     * @param DATE1
     * @param DATE2
     * @return 两个string类型的日期比较大小
     */
    public static int compare_date(String DATE1, String DATE2) {
         DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         try {
             Date dt1 = df.parse(DATE1);
             Date dt2 = df.parse(DATE2);
             if (dt1.getTime() > dt2.getTime()) {
                 System.out.println("dt1 在dt2前");
                 return 1;
             } else if (dt1.getTime() < dt2.getTime()) {
                 System.out.println("dt1在dt2后");
                 return -1;
             } else {
                 return 0;
             }
         } catch (Exception exception) {
             exception.printStackTrace();
         }
         return 0;
     }

    /**
     *
     * @param startTime
     * @param endTime
     * @return 两个string类型日期相差的小时数
     */
    public static int daysBetween2(String startTime, String endTime) {
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");  
          Calendar cal = Calendar.getInstance();    
          long time1 = 0;
          long time2 = 0;

          try{
               cal.setTime(sdf.parse(startTime));   
               time1 = cal.getTimeInMillis();    
               cal.setTime(sdf.parse(endTime)); 
               time2 = cal.getTimeInMillis();  
          }catch(Exception e){
              e.printStackTrace();
          }
          long between_days=(time2-time1)/(1000*3600);  

         return Integer.parseInt(String.valueOf(between_days));     
      }

    /**
     * 计算两段日期的重合日期
     * @param str1 开始日期1
     * @param str2 结束日期1
     * @param str3 开始日期2
     * @param str4 结束日期2
     * @return
     * @throws Exception
     */
    public static Map<String,Object> comparisonRQ(String str1, String str2, String str3,
            String str4) throws Exception {
        String mesg = "";
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String startdate = "";
        String enddate = "";
        try {
            Date dt1 = df.parse(str1);
            Date dt2 = df.parse(str2);
            Date dt3 = df.parse(str3);
            Date dt4 = df.parse(str4);
            if (dt1.getTime()<=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
                mesg = "f";//重合
                startdate = str3;
                enddate = str2;
            }
            if (dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()) {
                mesg = "f";//重合
                startdate = str1;
                enddate = str2;
            }

            if (dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
                mesg = "f";//重合
                startdate = str1;
                enddate = str4;
            }
            if (dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()) {
                mesg = "f";//重合
                startdate = str3;
                enddate = str4;
            }

            System.out.println(startdate+"----"+enddate);

        }catch (ParseException e) {
            e.printStackTrace();
            throw new ParseException(e.getMessage(), 0);
        }catch(Exception e){
            e.printStackTrace();
            throw new Exception(e);
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("startdate", startdate);
        map.put("enddate", enddate);
        return map;
    }