Java to judge two date difference days _java

Source: Internet
Author: User
Tags date1 diff time in milliseconds

The example in this article describes a method for Java to determine the number of days between two dates. Share to everyone for your reference. as follows:

 import Java.util.Calendar; public class datedifferent{public static void Main (string[) args) {
 Calendar calendar1 = Calendar.getinstance ();
 Calendar calendar2 = Calendar.getinstance ();
 Calendar1.set (2007, 01, 10);
 Calendar2.set (2007, 07, 01);
 Long milliseconds1 = Calendar1.gettimeinmillis ();
 Long milliseconds2 = Calendar2.gettimeinmillis ();
 Long diff = milliseconds2-milliseconds1;
 Long diffseconds = diff/1000;
 Long diffminutes = diff/(60 * 1000);
 Long diffhours = diff/(60 * 60 * 1000);
 Long DiffDays = diff/(24 * 60 * 60 * 1000);
 System.out.println ("\nthe Date different Example");
 System.out.println ("Time in milliseconds:" + diff + "milliseconds.");
 System.out.println ("Time in seconds:" + DiffSeconds + "seconds.");
 System.out.println ("Time in minutes:" + diffminutes + "minutes.");
 System.out.println ("Time in Hours:" + diffhours + "hours."); System.out.println ("Time in": "+ DiffDays +");} 

Put the above code in the project to use a bit, "The date part" is to full 24 hours to calculate a day, not too adapt to the needs of the project, so changed to such a son.

/**
 * Get two days difference between day *
/public static int Getbetweenday (date date1, date date2) {
  Calendar D1 = new Gregorian Calendar ();
  D1.settime (date1);
  Calendar D2 = new GregorianCalendar ();
  D2.settime (date2);
  int days = D2.get (calendar.day_of_year)-D1.get (calendar.day_of_year);
  System.out.println ("days=" +days);
  int y2 = d2.get (calendar.year);
  if (D1.get (calendar.year)!= y2) {
//      D1 = (Calendar) d1.clone ();
    Do {days
      + + d1.getactualmaximum (calendar.day_of_year);
      D1.add (calendar.year, 1);
    } while (D1.get (calendar.year)!= y2);
  }
  return days;
}

I hope this article will help you with your Java programming.

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.