Common Java Date comparison and date calculation methods summary _java

Source: Internet
Author: User
Tags current time date1 dateformat

This article for everyone to share is the commonly used date comparison method, for future reference.

Warm-up: get current time

SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
String nowdate = Df.format (new date ())//new date () to get current system time

Note: The landlord has always thought that the date type is not as arbitrary as the string type, so the following comparisons are all string-type date comparisons. If you really are that stubborn, OK, here's how to date to string:

SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//Set Date format
String date = Df.format (time of date type);

1. Two string-type date comparison size

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 before DT2");
         return 1;
       } else if (Dt1.gettime () < Dt2.gettime ()) {
         System.out.println ("dt1 after DT2");
         return-1;
       } else {return
         0;
       }
     catch (Exception Exception) {
       exception.printstacktrace ();
     }
     return 0;
   }

2. Returns the number of days between two string dates

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. Returns the number of hours of the two string type date difference

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. Calculate the date of overlap between two periods

/** * Calculates the coincidence date of two dates * @param str1 start date 1 * @param str2 End date 1 * @param str3 start date 2 * @param STR4 End Date 2 * @re Turn * @throws Exception */public static map<string,object> Comparisonrq (String str1, String str2, string s
    TR3, 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";//coincident startdate = STR3;
      EndDate = str2; } if (Dt1.gettime () >=dt3.gettime () &&dt3.gettime () <=dt2.gettime () &&dt2.gettime () <=
        Dt4.gettime ()) {MESG = "F";//coincident startdate = str1;
      EndDate = str2; } if (dt3.geTtime () <=dt1.gettime () &&dt1.gettime () <=dt4.gettime () &&dt4.gettime ()) {
        MESG = "F";//coincident startdate = str1;
      EndDate = STR4; } if (Dt3.gettime () >=dt1.gettime () &&dt1.gettime () <=dt4.gettime () &&dt4.gettime () <=
        Dt2.gettime ()) {MESG = "F";//coincident 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;
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.