PackageCommon classes. Date;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;/**The month is represented by an integer of 0-11, 0 is January, and 11 is the December * Date Object--The Millisecond value gettime method: * Reason: The millisecond value can be used to perform date operations * Millisecond values--"Date object SetTime method or constructor method new date (millisecond value); * Reason: You can perform the operation of a field such as month and day. * Date implements the comparable interface * format: simply say to re-partition the object, change the internal spatial structure of the object * abstract DateFormat: Date Format class * Factory: The place of production object*/ Public classDatedemo { Public Static voidMain (string[] args)throwsParseException {//demo_1 ();demo_2 (); Demo_3 (); Test_1 (); } Public Static voidTest_1 ()throwsparseexception {String str_day_1= "2016-5-1"; String str_day_2= "2017-4-23"; DateFormat DateFormat=dateformat.getdateinstance (); Date Date1=Dateformat.parse (str_day_1); Date Date2=Dateformat.parse (str_day_2); Long L1=Date1.gettime (); Long L2=Date2.gettime (); //Date Subtraction Make sure the result is positiveSystem.out.println (Math.Abs (L1-L2)/1000/60/60/24+ "Day"); } /**---A date-formatted string "Date Object *"@throwsparseexception*/ Public Static voidDemo_3 ()throwsparseexception {String str_day= "2017-4-23"; String str_day_1= "---5-1"; DateFormat DateFormat=dateformat.getdateinstance (); DateFormat D1=NewSimpleDateFormat ("YYYY---mm-dd"); Date Date1=Dateformat.parse (Str_day); System.out.println (date1); Date Date2=D1.parse (str_day_1); System.out.println (DATE2); } /**---the Date object to a date format string; Format a Date object using the Format method in the Dateformate class*/ Public Static voiddemo_2 () {Date Date=NewDate (); //gets the method of the date format object, with the default style. //DateFormat DateFormat = Dateformat.getdateinstance ();DateFormat DateFormat =dateformat.getdateinstance (dateformat.full); DateFormat=dateformat.getdatetimeinstance (Dateformat.long, Dateformat.long); String Str_day=Dateformat.format (date); System.out.println (Str_day); //custom style dates are done by the SimpleDateFormat classdateformat=NewSimpleDateFormat ("Yyyy--mm--dd"); String str_day_1=Dateformat.format (date); System.out.println (str_day_1); } Private Static voiddemo_1 () {Long time=System.currenttimemillis (); System.out.println (time); Date Date1=NewDate (); System.out.println (date1); Date Date2=NewDate (1492942746472L);//Long TypeSystem.out.println (DATE2); }}
PackageCommon classes. Calendar;ImportJava.util.Calendar;Importjava.util.Date;/*** Abstract Calendar class provides methods for returning objects getinstance ();*/ Public classCalendardemo { Public Static voidMain (string[] args) {Calendar C=calendar.getinstance (); //Set the time//C.set (Calendar.year, 2019); //C.set (2017, 3, 23); //Time Offset//C.add (Calendar.day_of_month, 9); //C.add (Calendar.year,-5); //Print Current TimeDemo_1 (c); //calculate how many days a year February intyear=2017; intday=demo_2 (c,year); System.out.println ( Year+ "+day+" Day of the Year); //get this time of yesterday//Demo_3 (c);//Date date = C.gettime ();//System.out.println (date); } Public Static voidDemo_3 (Calendar c) {//C.get (calendar.day_of_month);C.add (Calendar.day_of_month, 1); Demo_1 (c); } Private Static intDemo_2 (Calendar C,intYear ) {C.set (year,2, 1, 0, 0, 0); C.add (Calendar.day_of_month,-1); intDay =C.get (Calendar.day_of_month); returnDay ; } Public Static voiddemo_1 (Calendar c) {//Get current Time intYear =C.get (calendar.year); intDay =C.get (Calendar.day_of_month); intmonth = C.get (calendar.month) + 1;//the month starts at 0, so add 1 . intWeek =C.get (Calendar.day_of_week); intHour=C.get (Calendar.hour_of_day); System.out.println ( Year+ "Year" + Month + "Month" + Day + "date" +hour+ "point" +Getweek (week)); }/**output Date Be sure to note that 1 represents Sunday 7 for Saturday for viewing convenience you can implement the string output by the following method in the corresponding week*/ Private StaticString Getweek (intweek) {string[] arr= {"", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; returnArr[week]; }}
Java===date,dateformat,simpledateformat,calendar class