Java Calender Class Explanation

Source: Internet
Author: User

First, how to Create a Calendar object

The calendar is an abstract class that cannot be instantiated directly to get an object . Therefore , Calendar provides a way to getinstance to get a Calendar object that gets the calendar initialized by the current time .

1 Calendar cal = Calendar.getinstance ();
Second, the field detailed

The set and get times in the Calendar are implemented by filling in the parameters with different fields , but the constant values of some fields are not the same as the conventional ones .

The following fields we take 1998 February 28th 23 o'clock 11 minutes 11 seconds To explain

1 Calendar cal = calendar.getinstance (); 2 Cal.set (Calendar.year, 1998); 3 Cal.set (calendar.month, 1); 4 cal.set (calendar.date); 5 Cal.set (Calendar.hour_of_day, 23°c); 6 Cal.set (Calendar.minute, one); 7 Cal.set (Calendar.second, 11);
1. Year

The year is indicated by default .

For example, year is 1998

2. MONTH

Indicates the number of months in the current year , and the value of January in the year is 0

Note The January, February, March, APRIL, May, JUNE, July, August, September, October, November, December respectively with [0, 11 ] , be careful when setting up and getting dates

3. week_of_year

Indicates the number of weeks in the current year . The first week of the year has a value of 1.

In the example, week_of_year is 9 .

4. week_of_month

Indicates the number of weeks in the current month . The value of one weeks in one months is 1.

In the example, Week_of_month is 5 .

5. day_of_month

Indicates the day of the one month . It is synonymous with DATE. the value for the first day of one months is 1.

For example, day_of_month

6. DATE

With Day_of_month

7. day_of_year

Indicates the number of days in the current year . The value of the first day of the year is 1.

In the example, day_of_year is

8. Day_of_week

Indicates the day of the one week , the value of the first day of the week is 1.

It is important to note that, unlike Chinese habits , the first day of the week is Sunday . The order of the week is : Sunday (1), Monday (2), Tuesday (3), Wednesday c15> (4), Thursday (5), Friday (6), Saturday (7) pay extra attention when setting up and getting dates

9. am_pm

Indicates whether the HOUR is before noon or after noon . [0, 12] for the morning , the am_pm value is 1 [12, 24] for the afternoon , the am_pm value is 0

Ten. HOUR

Indicates the hours of the morning or afternoon .

For example, HOUR

hour_of_day .

Indicates the hour of the day. Hour_of_day for 24-hour clock

In the example, hour_of_day is

MINUTE .

Indicates the minutes in an hour

For example, MINUTE

SECOND .

Indicates the seconds in a minute

For example, SECOND

third, set the value
1Cal.set (2013, 5, 4, 13, 44, 51);//Date and time (month 0 stands for January)2Cal.set (Calendar.year, 2014);//years3Cal.set (Calendar.month, 7);//month (month 0 stands for January)4Cal.set (Calendar.date, 11);//Day5Cal.set (Calendar.hour_of_day, 15);//when6Cal.set (Calendar.minute, 33);//points7Cal.set (Calendar.second, 32);//seconds
Iv. getting values
1Cal.get (calendar.year);//years2Cal.get (Calendar.month) + 1;//month (must be + 1)3Cal.get (calendar.date);//Day4Cal.get (Calendar.hour_of_day);//when5Cal.get (Calendar.minute);//points6Cal.get (Calendar.second);//seconds7Cal.get (Calendar.day_of_week);//Week (Locale.english case, Sunday is 1, left to own projections)
Five, the operation value1. Add
1Cal.add (calendar.year, 1);//years2Cal.add (Calendar.month, 1);//Month3Cal.add (calendar.date, 1);//Day4Cal.add (Calendar.hour_of_day,-1);//when5Cal.add (Calendar.minute, 1);//points6Cal.add (Calendar.second, 1);//seconds7Cal.add (Calendar.date, 7);//Week

Vi. comparison between Calendar objects

Compares the time value represented by two calendar objects, returns a value of 0 if the time represented by the parameter equals the time represented by this calendar, or returns a value less than 0 if the time of the calendar is before the time represented by the parameter, if the time of the calendar is After that, a value greater than 0 is returned.

Java Calender Class Explanation

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.