常用的java日期比较和日期计算方法小结

时间:2022-04-21 00:26:11

本文为大家分享的都是常用的日期之间的比较方法,供以后参考。 

热身:获取当前时间 

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类型的时间);

1.两个string类型的日期比较大小 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
   }

2.返回两个string类型日期之间相差的天数 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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));  
   }

3.返回两个string类型日期相差的小时数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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));  
   }

4.计算两段日期的重合日期 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
   * 计算两段日期的重合日期
   * @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;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。