Date class, Calendar class
Package Box1;import Java.text.parseexception;import Java.text.simpledateformat;import java.util.Calendar;import Java.util.date;import Java.util.Random;//Date Class Public classDatetest { Public Static voidMain (string[] args) {Date date=NewDate (); System. out. println (date); //System.out.println (Date.getyear ()); //format TimeSimpleDateFormat sf=NewSimpleDateFormat ("yyyy year MM-DD hh:mm:ss E"); String datestring=Sf.format (date); System. out. println (datestring); //Exercise: Write a method that automatically generates the order number, rule: the first 2 characters are JE//The back 14 bits are the current date and time, followed by a four-bit random number. //Example: je201708291456201890SimpleDateFormat Change=NewSimpleDateFormat ("YYYYMMDDHHMMSS"); String ChangeDate=Change.format (date); Random a=NewRandom (); intA1 = A.nextint (Ten); intA2 = A.nextint (Ten); intA3 = A.nextint (Ten); inta4 = A.nextint (Ten); System. out. println ("JE"+changedate+a1+a2+a3+A4); //The calendar is an abstract class that cannot be instantiated,//a static method that can be instantiated by invoking a static method to produce an object, commonly called a factory methodCalendar c=calendar.getinstance (); System. out. print (c.Get(calendar.year) +"-"); System. out. print (c.Get(Calendar.month) +1+"-"); System. out. print (c.Get(calendar.date) +" "); System. out. print (c.Get(Calendar.hour) +":"); System. out. print (c.Get(Calendar.minute) +":"); System. out. print (c.Get(Calendar.second) +" "); System. out. println (c.Get(Calendar.day_of_week));//Sunday is 1//Date and calendar transferDate D1 =C.gettime (); System. out. println (Sf.format (D1)); C.settime (date); //Calculation of datesCalendar C1 =calendar.getinstance (); //first parameter: To add that field//second parameter: How much to addC1.add (Calendar.date, -); Date D2=C1.gettime (); System. out. println (Sf.format (D2)); //Date and string cross-transfer//Date Turn string with SimpleDateFormatDate D3 =NewDate ("2017/8/9");//incorrect format will cause error LongD4= Date.parse ("2017/10/10"); Date d5=NewDate (); D5.settime (D4); //turn a string into dateSimpleDateFormat format =NewSimpleDateFormat ("YYYY-MM-DD"); Try{Date d6=format.parse ("2017-08-09"); System. out. println (d6); } Catch(ParseException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Calendar C2=calendar.getinstance (); C2.Set( .,9, -, -, -, A); }}
Result diagram:
Enumeration
Package Box1; // enumeration: Representing constants, improving the readability of code Public enum Week { Mon,tue,wed,thu,sat,sun}
Package Box1; Public classWeektestenum { Public Static voidMain (string[] args) {System. out. println (Week.mon); Week N=Week.mon; Switch(n) { CaseMon:system. out. println ("Monday"); Break; CaseTue:system. out. println ("Tuesday"); Break; default: Break; } }}
Result diagram:
Singleton mode (emphasis)
Java Stream (stream), file and IO
Package Com.jredu.iodemo;//Java Stream (stream), file, and IoImport Java.io.fileinputstream;import Java.io.filenotfoundexception;import java.io.fileoutputstream;import java.io.IOException; Public classIotest { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("G:/io Data/io.text.txt");//calling the document//documents under the disk directory byte[]data =New byte[1024x768]; intLen; while(Len=fis.read (data))!=-1) {String s=NewString (data,0, Len); System. out. println (s); } fis.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Try{FileOutputStream fos=NewFileOutputStream ("G:/io Data/io.text.txt"); Fos.write ("ABC". GetBytes ());//direct coverage of content insideFos.flush (); Fos.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Result diagram::
Package Com.jredu.iodemo;import Java.io.fileinputstream;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import java.io.IOException; Public classFilecopydemo { Public Static voidMain (string[] args) {Try{FileInputStream fis=NewFileInputStream ("G:/io Data/123.png"); FileOutputStream Fos=NewFileOutputStream ("G:/io Data/1234.png"); byte[]data =New byte[1024x768]; intLen; Try { while(Len=fis.read (data))!=-1) {fos.write (data,0, Len); } fis.close (); Fos.flush (); Fos.close (); System. out. println ("Copy ok!"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Result diagram:
Java Learning Record (Supplement eight: Date class; Java Stream (stream), files (file), and IO)