Java date comparison, date calculation, and java date comparison Calculation

Source: Internet
Author: User
Tags date1

Java date comparison, date calculation, and java date comparison Calculation

They are common comparison methods between dates for future reference.

Warm-up: Get the current time

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // you can specify the date format.
String nowDate = df. format (new Date (); // new Date () is used to obtain the current system time.

Note: The landlord always thinks that the date type is not as good as the string type, so the following comparison is a string type date for comparison. If you are really stubborn, OK, the following is the method to convert date to string:

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // you can specify the date format.
String date = df. format (time of Date type );

1. Compare the size of two string-type dates

1 public static int compare_date (String DATE1, String DATE2) {2 DateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss "); 3 try {4 Date dt1 = df. parse (DATE1); 5 Date dt2 = df. parse (DATE2); 6 if (dt1.getTime ()> dt2.getTime () {7 System. out. println ("dt1 before dt2"); 8 return 1; 9} else if (dt1.getTime () <dt2.getTime () {10 System. out. println ("dt1 after dt2"); 11 return-1; 12} else {13 return 0; 14} 15} catch (Exception exception) {16 Exception. printStackTrace (); 17} 18 return 0; 19}

2. returns the number of days between two string-type dates.

 1  public static int daysBetween(String smdate,String bdate){   2          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");   3          Calendar cal = Calendar.getInstance();     4          long time1 = 0; 5          long time2 = 0; 6           7          try{ 8               cal.setTime(sdf.parse(smdate));    9               time1 = cal.getTimeInMillis();    10               cal.setTime(sdf.parse(bdate)); 11               time2 = cal.getTimeInMillis();  12          }catch(Exception e){13              e.printStackTrace();14          }15          long between_days=(time2-time1)/(1000*3600*24);  16              17         return Integer.parseInt(String.valueOf(between_days));     18      }  

3. Return the hours of two string-type date differences

 1  public static int daysBetween2(String startTime, String endTime) { 2           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");   3           Calendar cal = Calendar.getInstance();     4           long time1 = 0; 5           long time2 = 0; 6            7           try{ 8                cal.setTime(sdf.parse(startTime));    9                time1 = cal.getTimeInMillis();    10                cal.setTime(sdf.parse(endTime)); 11                time2 = cal.getTimeInMillis();  12           }catch(Exception e){13               e.printStackTrace();14           }15           long between_days=(time2-time1)/(1000*3600);  16               17          return Integer.parseInt(String.valueOf(between_days));     18       }

4. Calculate the coincidence date of the two segments

1/** 2 * calculate the coincidence date of the two dates 3 * @ param str1 start date 1 4 * @ param str2 end date 1 5 * @ param str3 start date 2 6 *@ param str4 end date 2 7 * @ return 8 * @ throws Exception 9 */10 public static Map <String, object> comparisonRQ (String str1, String str2, String str3, 11 String str4) throws Exception {12 String mesg = ""; 13 DateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); 14 String startdate = ""; 15 String enddate = ""; 16 try {17 Date dt1 = df. parse (str1); 18 Date dt2 = df. parse (str2); 19 Date dt3 = df. parse (str3); 20 Date dt4 = df. parse (str4); 21 if (dt1.getTime () <= dt3.getTime () & dt3.getTime () <= dt2.getTime () & dt2.getTime () <= dt4.getTime ()) {22 mesg = "f"; // coincidence 23 startdate = str3; 24 enddate = str2; 25} 26 if (dt1.getTime () >= dt3.getTime () & dt3.getTime () <= dt2.getTime () & dt2.getTime () <= dt4.getTime () {27 mesg = "f"; // coincidence 28 startdate = str1; 29 enddate = str2; 30} 31 32 if (dt3.getTime () <= dt1.getTime () & dt1.getTime () <= dt4.getTime () & dt4.getTime () <= dt2.getTime ()) {33 mesg = "f"; // coincidence 34 startdate = str1; 35 enddate = str4; 36} 37 if (dt3.getTime () >= dt1.getTime () & dt1.getTime () <= dt4.getTime () & dt4.getTime () <= dt2.getTime () {38 mesg = "f"; // coincidence 39 startdate = str3; 40 enddate = str4; 41} 42 43 System. out. println (startdate + "----" + enddate); 44 45 46} catch (ParseException e) {47 e. printStackTrace (); 48 throw new ParseException (e. getMessage (), 0); 49} catch (Exception e) {50 e. printStackTrace (); 51 throw new Exception (e); 52} 53 Map <String, Object> map = new HashMap <String, Object> (); 54 map. put ("startdate", startdate); 55 map. put ("enddate", enddate); 56 return map; 57}

 

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.