Java date plus subtraction

Source: Internet
Author: User
Tags constructor final

Sometimes, we want to add a few days, months, or years on the current date, but always find the right method, but under, please an expert help, Lao Liu gave a word: use Canlender. After that, I used calender, a use, really good use, write it, and we share together:
My needs
I am working on a questionnaire to set the effective number of days for a questionnaire: how to implement it within 30 days of the date of release?
My implementation

/*
* @author Easy
* 2005-12-14
*/
Calendar calendar = Calendar.getinstance ();
The GetTime () method is to get the current date whose return value is an object of a Java.util.Date class
Res.setresopendate (Calendar.gettime ());
int day = Calendar.get (calendar.day_of_year);
Calendar.set (calendar.day_of_year, day + 30);
Voting is valid for 30 days
Res.setresenddate (Calendar.gettime ());
² Detailed Description
⑴calendar is a class in java.util.*, so we have to import the package java.util.* before using it.
To produce an object of a calendar class, we cannot construct an instance with the new calendar (), because the//calendar constructor is protected, that is, the constructor is a protected member.
⑵ about the calendar filed
In Calendar, you define the members of the calendars, such as: year, month, day, including different methods of recording, for example: On the same day (2005-12-14), we can say that it is Wednesday of the week, the 14th of December, the first * * * * in 2005. (You can count your own) days. This is a different method of recording, so there are different definitions in the calendar: public final static int ERA = 0;
Public final static int year = 1;
Public final static int MONTH = 2;
Public final static int week_of_year = 3;
Public final static int week_of_month = 4;
Public final static int DATE = 5;
Public final static int day_of_month = 5;
Public final static int day_of_year = 6;
Public final static int day_of_week = 7;
Public final static int day_of_week_in_month = 8;
Public final static int am_pm = 9;
Public final static int HOUR = 10;
Public final static int hour_of_day = 11;
Public final static int MINUTE = 12;
Public final static int SECOND = 13;

These definitions are the identification of different recording methods, which I do not enumerate completely, interested you can look at the source code of the calendar.
These are the field of the calendar, according to your different needs can go to different field, different record method is also different filed, this should be good to understand some.
With this, we can get whatever you want, and if you want to know what day of the year it is, you can use:
int day = Calendar.get (calendar.day_of_year);
Parameters are the field you want to get, all of which are defined in the calendar.
The results will tell us that 2005-12-14 is the No. 348 day of 2005.
And you want to know what the date is in 30 days?
Calendar.set (calendar.day_of_year, day + 30);
In this, you don't have to consider 12-14 plus 30 days is January, is not going to go wrong, will not, calendar itself know that day is a few months date.

So you can know the date when you get the date. Of course, you can also directly through the calendar to obtain.
I will not say the other, is the same truth. In this way, we can use the calendar implementation date of the random addition.
The above is my personal experience, if you have a better understanding, may share. Deficiencies, but also look at the guidance of the viewer.

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.