Calculate the number of days between two times

Source: Internet
Author: User
Tags date1


1 The first Kind

Import java.text.ParseException;

Import Java.text.SimpleDateFormat;

Import Java.util.Calendar;

Import Java.util.Date;

public class Test {

public static void Main (string[] args) {

try {

Time Conversion class

SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");

Date date1 = Sdf.parse ("2014-1-10");

Date date2 = Sdf.parse ("2012-12-20");

Converts two time objects converted to Calendard objects

Calendar can1 = Calendar.getinstance ();

Can1.settime (date1);

Calendar can2 = Calendar.getinstance ();

Can2.settime (DATE2);

Take out two years

int year1 = Can1.get (calendar.year);

int year2 = Can2.get (calendar.year);

Days

int days = 0;

Calendar can = null;

If Can1 < can2

Minus the number of days that a small time has elapsed in this year

Plus the number of days that the big time has elapsed

if (Can1.before (can2)) {

Days-= Can1.get (calendar.day_of_year);

Days + = Can2.get (calendar.day_of_year);

can = Can1;

}else{

Days-= Can2.get (calendar.day_of_year);

Days + = Can1.get (calendar.day_of_year);

can = can2;

}

for (int i = 0; i < Math.Abs (year2-year1); i++) {

Get the total number of days in a small time current year

Days + = Can.getactualmaximum (calendar.day_of_year);

And calculate the next year.

Can.add (calendar.year, 1);

}

System.out.println ("Days Difference:" +days);

} catch (ParseException e) {

E.printstacktrace ();

}

}

}

Console:

Days difference: 386



2 the second type



First, the obtained string date is converted to the date type (the day from the foreground page is a string type, A, b):

Date A1 = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (a);

Date B1 = new SimpleDateFormat ("Yyyy-mm-dd"). Parse (b);

Gets the number of days after subtraction

Long day = (A1.gettime ()-b1.gettime ())/(24*60*60*1000);

I hope I can help you.



Third Kind



Number of days apart

Date D1 = new Date ();

Date D2 = new Date (2013,10,23,14,15,26);


int days = (int) ((D1.gettime ()-d2.gettime ())/86400000);


System.out.println ("interval:" + days);





The third way the transformation of the time a bit of a problem I hope who can help me to improve the next, the first and second can be converted but note that the second time to calculate the number of days need to distinguish the size of the time, otherwise it will be calculated negative.

This article is from the "11514077" blog, please be sure to keep this source http://11524077.blog.51cto.com/11514077/1837174

Calculate the number of days between two times

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.