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.