Java date addition and subtraction.

Source: Internet
Author: User

1. UseJava. Util. calender to implement

Calendar calendar = calendar. getinstance ();
Calendar. settime (new date ());
System. Out. println (calendar. Get (calendar. day_of_month); // today's date
Calendar. Set (calendar. day_of_month, calendar. Get (calendar. day_of_month) + 1); // Add 1 to the date.
System. Out. println (calendar. Get (calendar. Date); // Add the top date after 1
========================================================== ========================================================

2. UseJava. Text. simpledateformat andJava. Util. Date

Date d = new date ();
Simpledateformat df = new simpledateformat ("yyyy-mm-dd ");
System. Out. println ("today's date:" + DF. Format (d ));
System. out. println ("date two days ago:" + DF. format (new date (D. gettime ()-2*24*60*60*1000 )));
System. Out. println ("three days later:" + DF. Format (new date (D. gettime () + 3*24*60*60*1000 )));

========================================================== ========================================================

Gregoriancalendar GC = new gregoriancalendar ();
GC. settime (new date );
GC. Add (field, value );
Value is followed by a regular expression, and if it is negative, it goes forward.
Field Values: 1 plus 1 year, 2 plus half year, 3 plus 1 quarter, 4 plus 1 week
5 plus one day ....

========================================================== ========================================================

/*
*JavaAddition and subtraction of dates
* GC. Add (1,-1) indicates the year minus one.
* GC. Add (2,-1) indicates the month minus one.
* GC. Add (3.-1) indicates that one week is subtracted.
* GC. Add (5,-1) indicates a day minus one.
* Likewise, it should be accurate to milliseconds. If you haven't tried again, you can try it.
* The add (INT field, int amount) method of the gregoriancalendar class indicates addition and subtraction of year, month, and day.
* The field parameter indicates year, month, and day.
* The amount parameter indicates the quantity to be added or subtracted.
*
* Usedate.JavaThe test is as follows:
*/
Package temp. util;

Import
Java
. Util. calendar;
ImportJava. Util. date;
ImportJava. Util. gregoriancalendar;
ImportJava. Text. simpledateformat;
Public class usedate {

Date d = new date ();
Gregoriancalendar GC = new gregoriancalendar ();
Simpledateformat Sf = new simpledateformat ("yyyy-mm-dd ");

Public String getyears ()
{
GC. settime (d );
GC. Add (1, + 1 );
GC. Set (GC. Get (calendar. Year), GC. Get (calendar. month), GC. Get (calendar. Date ));

Return SF. Format (GC. gettime ());
}

Public String gethalfyear ()
{
GC. settime (d );
GC. Add (2, + 6 );
GC. Set (GC. Get (calendar. Year), GC. Get (calendar. month), GC. Get (calendar. Date ));

Return SF. Format (GC. gettime ());
}
Public String getquarters ()
{
GC. settime (d );
GC. Add (2, + 3 );
GC. Set (GC. Get (calendar. Year), GC. Get (calendar. month), GC. Get (calendar. Date ));

Return SF. Format (GC. gettime ());
}

Public String getlocaldate ()
{
Return SF. Format (d );
}

Public static void main (string [] ARGs)
{
Usedate UD = new usedate ();
System. Out. println (ud. getlocaldate ());
System. Out. println (ud. getyears ());
System. Out. println (ud. gethalfyear ());
System. Out. println (ud. getquarters ());
}

}

 

 

4,

Gregoriancalendar GC = new gregoriancalendar ();

Try {
GC. settime (New simpledateformat ("yyyymm"). parse ("200901 "));
GC. Add (2,-integer. parseint ("7 "));
} Catch (parseexception e ){
// Todo automatically generates automatically generated writable catch writable
E. printstacktrace ();
}
System. Out. println (New simpledateformat ("yyyymm"). Format (GC. gettime ()));

Result: 200806

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.