Calculates the difference of two dates

Source: Internet
Author: User

Package com.xcfh.util;

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
Import Java.util.GregorianCalendar;

/**
*
* @ClassName: Datemargin
*
* @Description: Calculates the difference of days between two dates
* @author Pinetree
* @date December 20, 2014 2:20:17
* @version
*/
public class Datemargin {

public static void Main (string[] args) {
try{
/**
* GregorianCalendar (int year, int month, int dayofmonth, int hourofday, int minute, int second)
* year-used to set the value of the year Calendar field in the calendar.
* Month-Used to set the value of the month calendar field in the calendar. The Month value is based on 0, for example, 0 for January.
* DayOfMonth-Used to set the value of the Day_of_month Calendar field in the calendar.
* Hourofday-Used to set the value of the Hour_of_day Calendar field in the calendar.
* minute-used to set the value of the minute Calendar field in the calendar.
* Second-used to set the value of the second Calendar field in the calendar.
*/
Date startdate = new GregorianCalendar (n, 0). GetTime ();
Date endDate = new GregorianCalendar (). GetTime ();
System.out.println (startdate + "\ T" + endDate);
System.out.println ("Number of days using Getintervaldays =" + getintervaldays (StartDate, endDate));
System.out.println ("Number of days using Daysoftwo =" + Daysoftwo (StartDate, endDate));
System.out.println ("Number of days using Daysbetween =" + Daysbetween (StartDate, endDate));
System.out.println ("Number of days using Daysmargin =" + Daysmargin (startdate.tolocalestring (), enddate.tolocalestring ()));
}catch (Exception e) {
E.printstacktrace ();
}
}

/**
* Calculates the number of days between two dates
*
* This version of the method to compare to the strict sense of the day, for example, the calculation today compared to yesterday, a few days,
* We may all think of it as 1, but we're going to enter two date-type parameters, and this date contains not only days,
* There are hours, minutes, etc., version 1 will be different depending on the hours you enter, return different results,
* For example, you enter the parameters today at noon and last night two time, it will tell you the difference of 0 days.
*
* @param startdate Start date
* @param endDate End Date
* @return int If yes-1 description start date or end date is null
*/
public static int Getintervaldays (date startdate, date endDate) {
if (null = = StartDate | | null = = endDate) {
return-1;
}
System.out.println ("endDate =" + enddate.gettime () + "\tstartdate =" + Startdate.gettime ());
Long Intervalmilli = Enddate.gettime ()-startdate.gettime ();
System.out.println ("Intervalmilli =" + Intervalmilli);
return (int) (Intervalmilli/(24 * 60 * 60 * 1000));
}

/**
* Calculates the number of days between two dates
*
* @param startdate Start date
* @param endDate End Date
* @return int If yes-1 description start date or end date is null
*/
public static int Daysoftwo (date startdate, date endDate) {
if (null = = StartDate | | null = = endDate) {
return-1;
}
Calendar calendar = Calendar.getinstance ();
Calendar.settime (StartDate);
int startday = Calendar.get (calendar.day_of_year);
System.out.println ("StartDay =" + StartDay);
Calendar.settime (endDate);
int endday = Calendar.get (calendar.day_of_year);
System.out.println ("Endday =" + Endday);
return endday-startday;
}

/**
* Calculate two date difference days, first turn the date into YYYY-MM-DD format, and then calculate.
*
* @param startdate Start date
* @param endDate End Date
* @return int If yes-1 description start date or end date is null
* @throws ParseException
*/
public static int Daysbetween (date startdate, date endDate) throws parseexception{
System.out.println (startdate.tolocalestring () + "\ T" + enddate.tolocalestring ());
if (null = = StartDate | | null = = endDate) {
return-1;
}
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
StartDate = Sdf.parse (Sdf.format (startdate));
EndDate = Sdf.parse (Sdf.format (endDate));
Calendar calendar = Calendar.getinstance ();
Calendar.settime (StartDate);
Long StartDay = Calendar.gettimeinmillis ();
Calendar.settime (endDate);
Long endday = Calendar.gettimeinmillis ();

return (int) ((endday-startday)/(24 * 60 * 60 *1000));
}

/**
* String type Date calculation difference days
*
* @param startdate Start date
* @param endDate End Date
* @return int If yes-1 description start date or end date is null
* @throws ParseException
*/
public static int Daysmargin (string startdate, String endDate) throws parseexception{
if (null = = StartDate | | null = = endDate) {
return-1;
}
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date newstartdate = Sdf.parse (startdate);
Date newenddate = Sdf.parse (endDate);
Calendar calendar = Calendar.getinstance ();
Calendar.settime (newstartdate);
Long StartDay = Calendar.gettimeinmillis ();
Calendar.settime (newenddate);
Long endday = Calendar.gettimeinmillis ();
return (int) ((endday-startday)/(24 * 60 * 60 *1000));
}
}



Calculates the difference of two dates

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.