Some common methods of Calendar tool classes in Java

Source: Internet
Author: User

One: Information for fields and methods
Year field:
public static final int year;
Indicates the get and set field numbers for the year. This is a calendar-specific value;
Get method:
public int get (int field);
Returns the value of the given Calendar field. In lenient mode, all calendar fields are normalized. In non-lenient mode, all calendar fields are validated, and if any calendar fields have out-of-range values, this method throws an exception. Normalization and validation are handled through the complete () method, which is related to the calendar system.
Parameters: field-the given calendar field.
Returns: The value of the given Calendar field.
Thrown: arrayindexoutofboundsexception-If the specified field is out of range (Field < 0 | | field >= field_count).
For more information, refer to the JDK API Java.util.Calendar

Second: Calendar class explanation
The Calendar class is an abstract class that provides methods for converting between a specific time and a set of calendar fields such as year, MONTH, Day_of_month, HOUR, and provides methods for manipulating calendar fields, such as getting the date of the next week. The instantaneous value is expressed as a millisecond, which is the offset from the calendar element (that is, Greenwich Mean Time January 1, 1970, 00:00:00.000, Gregorian calendar).

1, before using the Calendar class, you should know that some of the calendar methods to retrieve the number is corresponding to the calendar itself constant, that is, not to retrieve the number 1, it means that today is Monday.
A. To get time, first use calendar's getinstance () to get a calendar instance, for example:
Calendar RightNow = Calendar.getinstance ();
b, if you want to know for years now, you can use the Get () method and specify constants such as:
System.out.println (Rightnow.get (calendar.year));
C, if it is 2005, the previous meeting shows 2005 of the figures, according to this example, assuming that the time to write this article is May, and now want to use the program to obtain the month, the following example may be somewhat confusing:
System.out.println (Rightnow.get (calendar.month));
D, the program will show 4 this number, rather than the expected 5, because the return of 4 is not a representative of the month, but corresponds to the value of the Calendar.may constant, The constant value of calendar in the month starts from Calendar.january to 0, to Calendar.december 11, so if you want to display the true meaning of the return value, you can write as follows:
string[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
Calendar RightNow = Calendar.getinstance ();
System.out.println (Months[rightnow.get (calendar.month));

2. Initial value
The code below, it is worth pointing out is because our time zone setting is gmt+8, so the print GMT get is 1970-01-01 08:00:00.
Calendar cal = Calendar.getinstance ();//Get current time
Cal.settimeinmillis (0);//Convert to GMT

3. Get the value
Cal.get (calendar.year);//year
Cal.get (calendar.month) + 1;//Month (must + 1)
Cal.get (calendar.date);//day
Cal.get (calendar.hour_of_day);//
Cal.get (Calendar.minute);//min
Cal.get (Calendar.second);//sec
Cal.get (Calendar.day_of_week);//week (Locale.english case, Sunday is 1, left to calculate)
If take time is not to calculate but show out, certainly with Simpledateformart, mode for YYYY-MM-DD HH:mm:ss EE

4. Set the value
Cal.set (2013, 5, 4, 13, 44, 51);//Date and time (month 0 stands for January)
Cal.set (Calendar.year, 2014);//year
Cal.set (Calendar.month, 7);//month (month 0 stands for January)
Cal.set (Calendar.date, 11);//day
Cal.set (Calendar.hour_of_day, 15);//
Cal.set (Calendar.minute, 33);//min
Cal.set (Calendar.second, 32);//sec

5. Operation Value
Cal.add (calendar.year, 1);//year
Cal.add (Calendar.month, 1);//month
Cal.add (calendar.date, 1);//day
Cal.add (Calendar.hour_of_day,-1);//
Cal.add (Calendar.minute, 1);//min
Cal.add (Calendar.second, 1);//sec
Cal.add (Calendar.date, 7);//week

Some common methods of Calendar tool classes in Java

Related Article

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.