Home>
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 days:" + diffdays + "days.");
 }
}

Put the above code in the project and use it a bit,[The date part] It takes 24 hours to count as a day.Not well adapted to the needs of the project,So change it to this.

/**
 * Get the number of days between the two dates
 * /
public static int getbetweenday (date date1, date date2) {
  calendar d1=new gregoriancalendar ();
  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;
}
  • Previous C language fillpoly function
  • Next ThinkPHP lets /Public not parse (direct output) in templates