CopyCode The Code is as follows: Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;
Public class helloworld {
Public static void main (string [] ARGs ){
// Specify the time output format
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Date dt = new date ();
System. Out. println ("current time:" + SDF. Format (DT ));
Calendar rightnow = calendar. getinstance ();
Rightnow. settime (DT );
Rightnow. Add (calendar. Year,-1); // the current date minus one year
System. Out. println ("minus 1 year:" + SDF. Format (rightnow. gettime ()));
Rightnow. Add (calendar. Month, 3); // (in the result above) plus 3 months
System. Out. println ("plus 3 months:" + SDF. Format (rightnow. gettime ()));
Rightnow. Add (calendar. day_of_year, 10); // (in the result above) add 10 days
System. Out. println ("plus 10 days:" + SDF. Format (rightnow. gettime ()));
}
}
Output result:
Current Time: 09:40:49
Minus 1 year: 09:40:49
Plus 3 months: 09:40:49
Plus 10 days: 09:40:49