Java calendar time operations, the code is very simple.
This example shows how to get the time, how to accumulate and subtract the date and time.
Package mytest;/** demonstrate the general operation of calendar */import Java. text. simpledateformat; import Java. util. date; import Java. util. calendar; public class test {public test () {} public static void main (string [] ARGs) {// complete display Date and Time string STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS: SSS ")). format (new date (); system. out. println (STR); // create the Calendar Object calendar = calendar. getinstance (); // initialize the Calendar Object, but it is not necessary unless you need to reset the time calendar. settime (new date (); // settime is similar to the previous line // Date = new date (); // calendar. settime (date); // display the year int year = calendar. get (calendar. year); system. out. println ("year is =" + String. valueof (year); // display the month (starting from 0, actually show to add one) int month = calendar ar. get (calendar. month); system. out. println ("month is =" + (month + 1); // the nth day of the year int day_of_year = calendar. get (calendar. day_of_year); system. out. println ("day_of_year is =" + day_of_year); // the nth day of the month int day_of_month = calendar. get (calendar. day_of_month); system. out. println ("day_of_month =" + String. valueof (day_of_month); // calendar after 3 hours. add (calendar. hour_of_day, 3); int hour_of_day = calendar. get (calendar. hour_of_day); system. out. println ("hour_of_day + 3 =" + hour_of_day); // the current number of minutes int minute = calendar ar. get (calendar. minute); system. out. println ("minute =" + minute); // calendar in 15 minutes. add (calendar. minute, 15); minute = calendar. get (calendar. minute); system. out. println ("minute + 15 =" + minute); // calendar 30 minutes ago. add (calendar. minute,-30); minute = calendar. get (calendar. minute); system. out. println ("Minute-30 =" + minute); // formatted STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS ")). format (calendar. gettime (); system. out. println (STR); // reset the calendar to display the current time in the calendar. settime (new date (); STR = (New simpledateformat ("yyyy-mm-dd hh: mm: SS ")). format (calendar. gettime (); system. out. println (STR); // create a calendar to compare the time of calendar. getinstance (); // set to 5 hours ago, the latter is large, and-1 calendarnew is displayed. add (calendar. hour,-5); system. out. println ("time comparison:" + calendarnew. compareto (calendar); // After the value is set to 7 hours, the former is large and 1 calendarnew is displayed. add (calendar. hour, + 7); system. out. println ("time comparison:" + calendarnew. compareto (calendar); // returns two hours. The time is the same. 0 calendarnew is displayed. add (calendar. hour,-2); system. out. println ("time comparison:" + calendarnew. compareto (calendar ));}}