Calculate the number of days between two dates

Source: Internet
Author: User

Note: If you want to see the number of days between the two dates, you can directly jump to "5"; the previous step is the preparatory knowledge;

I. classes to be used

(1) Java. util. date;

(2) Java. util. calendar;

(3) Java. util. gregoriancalendar;

(4) Java. Text. simpledateformat;

Ii. Use of date

Since date is outdated, there are several points to use:

(1) obtain the current time: Date = new date ();

(2) Conversion between date and calendar (will be discussed later)

(3) simpledateformat parsing; (will be discussed later)

Iii. Use of calendar and gregoriancalendar

Calendar is widely used because it can easily obtain the required attributes, such as years, months, days, minutes, seconds, milliseconds, and weeks;

Common usage:

(1) gregoriancalendar calendar = new gregoriancalendar (); obtain the current time;

(2) calendar. settime (date); // set the date time to calendar;

(3) date calendar. gettime (); // convert calendar to date type;

(4) calendar. Get (attribute); // obtain the attribute value based on the name;

(5) calendar. gettimeinmillis (); // obtain the number of milliseconds for the calendar time. (This is the key to calculating the number of days for different dates)

Iv. simpledateformat

Simpledateformat is widely used because it can customize the date display format;

(1) y indicates the year; yyyy indicates the four-digit year, such as 2011

(2) M indicates month; mm indicates two months, for example, 11.

(3) d Indicates day; dd indicates two days, for example, 11.

Common usage:

(1) simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); // set the date display format to yyyy-mm-dd, for example

(2) date = SDF.Parse(String Str); // parses a string into a date of the date type. For example, "" is parsed into a date of the 2011-01-01 type;

(3) string STR = SDF.Format(Date); // format the date as a string;

5. Two-date interval days skill: calculate the number of days at the interval in milliseconds. If you know the number of milliseconds at the interval, (1) Long sec = Milli/1000; (2) long min = SEC/60; (3) long hour = min. /60; (4) Long Day = hour/24;

Package Org. exam3; import Java. text. simpledateformat; import Java. util. date; import Java. util. gregoriancalendar; import Java. util. principal; public class test {public static void main (string [] ARGs) throws exception {writable in = new writable (system. in); In. usedelimiter ("\ n"); system. out. print ("Start Time:"); string str1 = in. next (); system. out. println ("\ n end time:"); string str2 = in. next (); simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); Date date1 = SDF. parse (str1); Date date2 = SDF. parse (str2); gregoriancalendar cal1 = new gregoriancalendar (); gregoriancalendar cal2 = new gregoriancalendar (); cal1.settime (date1); cal2.settime (date2 );Long gap = (cal2.gettimeinmillis ()-cal1.gettimeinmillis ()/(1000*3600*24); // changes from the interval in milliseconds to the interval in daysSystem. Out. println ("\ n difference" + gap + "day ");}}

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: 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.