1 /* 2 * The general operation of the Calendar is shown3 */ 4 Importjava.util.Date; 5 ImportJava.text.SimpleDateFormat; 6 ImportJava.text.DateFormat; 7 ImportJava.util.Calendar; 8 9 Public classTestTen { One PublicTest () A { - } - the Public Static voidMain (string[] args) - { - //string Conversion date format -DateFormat Fmtdatetime =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); + //Get date format Objects -Date Date =Fmtdatetime.parse (Strdatemake); + A //Full display date time atString str = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SSS")). Format (NewDate ()); - System.out.println (str); - - //Create a Calendar object -Calendar Calendar =calendar.getinstance (); - //Initialize Calendar object, but not necessary unless reset time is required inCalendar.settime (NewDate ()); - to //settime Similar to the above line + //Date date = new Date (); - //calendar.settime (date); the * //Show Year $ intYear =Calendar.get (calendar.year); Panax NotoginsengSystem.out.println ("Year is =" +string.valueof (year)); - the //Display month (starting from 0, actual display to add one) + intMONTH =Calendar.get (Calendar.month); ASYSTEM.OUT.PRINTLN ("Month is =" + (month + 1)); the + //the nth day of the year - intDay_of_year =Calendar.get (calendar.day_of_year); $System.out.println ("day_of_year is =" +day_of_year); $ - //Nth day of the month - intDay_of_month =Calendar.get (Calendar.day_of_month); theSystem.out.println ("Day_of_month =" +string.valueof (day_of_month)); - Wuyi //after 3 hours theCalendar.add (Calendar.hour_of_day, 3); - intHour_of_day =Calendar.get (Calendar.hour_of_day); WuSystem.out.println ("Hour_of_day + 3 =" +hour_of_day); - About //current number of minutes $ intMINUTE =Calendar.get (Calendar.minute); -System.out.println ("MINUTE =" +MINUTE); - - //15 minutes later . ACalendar.add (Calendar.minute, 15); +MINUTE =Calendar.get (Calendar.minute); theSystem.out.println ("MINUTE + 15 =" +MINUTE); - $ //30 minutes ago theCalendar.add (Calendar.minute, 30); theMINUTE =Calendar.get (Calendar.minute); theSystem.out.println ("MINUTE-30 =" +MINUTE); the - //formatted display instr = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS") . Format (Calendar.gettime ()); the System.out.println (str); the About //Reset Calendar Show Current Time theCalendar.settime (NewDate ()); thestr = (NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss:SS") . Format (Calendar.gettime ()); the System.out.println (str); + - //Create a Calendar to compare time theCalendar calendarnew =calendar.getinstance (); Bayi the //set to 5 hours ago, the latter large, show-1 theCalendarnew.add (Calendar.hour, 5); -System.out.println ("Time Comparison:" +Calendarnew.compareto (calendar)); - the //set 7 hours later, the former large, showing 1 theCalendarnew.add (Calendar.hour, +7); theSystem.out.println ("Time Comparison:" +Calendarnew.compareto (calendar)); the - //return 2 hours, same time, display 0 theCalendarnew.add (Calendar.hour, 2); theSystem.out.println ("Time Comparison:" +Calendarnew.compareto (calendar)); the } 94}
[Java] Calendar