Common functions of date and time operations in Java

Source: Internet
Author: User
Common functions of date and time operations in Java

1. Calculate the maximum number of days in a month
Calendar time = calendar. getinstance ();
Time. Clear ();
Time. Set (calendar. year, year );
Time. Set (calendar. Month, I-1); // note that the Calendar Object is 0 by default
Int day = time. getactualmaximum (calendar. day_of_month); // number of days of the current month
Note: before using the set method, you must clear it first. Otherwise, many information will inherit from the current system time.

2. Conversion of calendar and date
(1) convert calendar to date
Calendar Cal = calendar. getinstance ();
Date = Cal. gettime ();
(2) convert date to calendar
Date = new date ();
Calendar Cal = calendar. getinstance ();
Cal. settime (date );

3. format the output Date and Time
Date = new date ();
Simpledateformat df = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
System. Out. println (DF. Format (date ));

4. calculate the day of the year
(1) Calculate the day of the year
calendar ar Cal = calendar ar. getinstance ();
Cal. set (calendar. year, 2006);
Cal. set (calendar. month, 8);
Cal. set (calendar. day_of_month, 3);
int weekno = Cal. get (calendar. week_of_year);
(2) number of weeks in a year
simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
calendar Cal = calendar. getinstance ();
Cal. set (calendar. year, 2006);
Cal. set (calendar. week_of_year, 1);
Cal. set (calendar. day_of_week, calendar. monday);
system. out. println (DF. format (Cal. gettime ();
output:
2006-01-02

5.add() and roll () usage
(1) Add () method
simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
calendar Cal = calendar. getinstance ();
Cal. set (calendar. year, 2006);
Cal. set (calendar. month, 8);
Cal. set (calendar. day_of_month, 3);
Cal. add (calendar. date,-4);
date = Cal. gettime ();
system. out. println (DF. format (date);
Cal. add (calendar. date, 4);
date = Cal. gettime ();
system. out. println (DF. format (date);
output:
(2) Roll method
Cal. set (calendar. year, 2006);
Cal. set (calendar. month, 8);
Cal. set (calendar. day_of_month, 3);
Cal. roll (calendar. date,-4);
date = Cal. gettime ();
system. out. println (DF. format (date);
Cal. roll (calendar. date, 4);
date = Cal. gettime ();
system. out. println (DF. format (date);
output:
2006-09-29
2006-09-03
visible. The roll () method loops in the current month. The add () method is generally used;

6. Calculate the number of days between two arbitrary periods.
(1) upload to the Calendar Object
/***** // Calculate the number of days between two time periods
* @ Param startday Start Time
* @ Param endday End Time
* @ Return
*/
Public int getintervaldays (calendar startday, calendar endday )...{
// Make sure that startday is before endday
If (startday. After (endday ))...{
Calendar Cal = startday;
Startday = endday;
Endday = Cal;
}
// Obtain the number of milliseconds for each time.
Long SL = startday. gettimeinmillis ();
Long El = endday. gettimeinmillis ();

Long Ei = El-Sl;
// Calculate the number of days in milliseconds
Return (INT) (Ei/(1000*60*60*24 ));
}
(2) input date object
/***** // Calculate the number of days between two time periods
* @ Param startday Start Time
* @ Param endday End Time
* @ Return
*/
Public int getintervaldays (date startday, date endday )...{
// Make sure that startday is before endday
If (startday. After (endday ))...{
Date Cal = startday;
Startday = endday;
Endday = Cal;
}
// Obtain the number of milliseconds for each time.
Long SL = startday. gettime ();
Long El = endday. gettime ();

Long Ei = El-Sl;
// Calculate the number of days in milliseconds
Return (INT) (Ei/(1000*60*60*24 ));
}
Similarly, you can use the same method to calculate the number of hours, minutes, And seconds between two different time periods.
Note: The above method is completely time-based, and sometimes it is not satisfactory, for example:
Startday = "20:00:00"
Endday = "8:00:00"
The calculation result is 0, but we may change the calculation result to 1. You can use the following method to achieve this:
Before passing parameters, set the endday time, for example:
Endday. Set (calendar. hour_of_day, 23 );
Endday. Set (calendar. Minute, 59 );
Endday. Set (calendar. Second, 59 );
Endday. Set (calendar. millisecond, 59 );
In this way, input startday and endday, and the result will be as expected. However, if the above method is too difficult, you can refer to the following method:
(3) improved methods for accurate calculation of days separated
Public int getdaysbetween (calendar D1, calendar D2 )...{
If (d1.after (D2)... {// swap dates so that D1 is start and D2 is end
Java. util. Calendar swap = D1;
D1 = d2;
D2 = swap;
}
Int days = d2.get (calendar. day_of_year)-d1.get (calendar. day_of_year );
Int y2 = d2.get (calendar. year );
If (d1.get (calendar. Year )! = Y2 )...{
D1 = (calendar) d1.clone ();
Do ...{
Days + = d1.getactualmaximum (calendar. day_of_year); // obtain the actual number of days of the current year.
D1.add (calendar. Year, 1 );
} While (d1.get (calendar. Year )! = Y2 );
}
Return days;
}

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.