Java學習記錄(補充八:Date類;Java流(Stream),文件(File)和IO)
阿新 • • 發佈:2017-08-31
tac hang set .get com 文件 pac 抽象類 格式化
Date類,Calendar類
package Box1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; //Date類 public class DateTest { public static void main(String[] args) { Date date = new Date(); System.out.println(date); //System.out.println(date.getYear()); //格式化時間 SimpleDateFormat sf= new SimpleDateFormat("yyyy年MM-dd hh:mm:ss E"); String dateString = sf.format(date); System.out.println(dateString); //練習:寫一個方法,自動生成定單編號,規則:前2個字符為je//後邊14位為當前日期及時間,後面是四位隨機數. //例如:je201708291456201890 SimpleDateFormat change = new SimpleDateFormat("yyyyMMddhhmmss"); String changeDate = change.format(date); Random a = new Random(); int a1 = a.nextInt(10); int a2 = a.nextInt(10); int a3 = a.nextInt(10);int a4 = a.nextInt(10); System.out.println("je"+changeDate+a1+a2+a3+a4); //Calendar是抽象類,不能實例化, //能通過調用靜態方法來實例化,用來產生對象的靜態方法,一般稱為工廠方法 Calendar 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));//星期日是1 //date與calendar互轉 Date d1 = c.getTime(); System.out.println(sf.format(d1)); c.setTime(date); //日期的計算 Calendar c1 = Calendar.getInstance(); //第一個參數:要加那個字段 //第二個參數:要加多少 c1.add(Calendar.DATE, 100); Date d2 = c1.getTime(); System.out.println(sf.format(d2)); //日期與字符串互轉 //Date轉String用SimpleDateFormat Date d3 = new Date("2017/8/9");//格式不對會報錯 long d4= Date.parse("2017/10/10"); Date d5 = new Date(); d5.setTime(d4); //把字符串轉成Date SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date d6=format.parse("2017-08-09"); System.out.println(d6); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar c2 = Calendar.getInstance(); c2.set(2017, 9, 27, 20, 20,12); } }
結果圖:
枚舉
package Box1; //枚舉:表示常量,提高代碼的可讀性 public enum Week { MON,TUE,WED,THU,SAT,SUN }
package Box1; public class WeekTestEnum { public static void main(String[] args) { System.out.println(Week.MON); Week n =Week.MON; switch (n) { case MON: System.out.println("周一"); break; case TUE: System.out.println("周二"); break; default: break; } } }
結果圖:
單例模式(重點)
Java流(Stream),文件(File)和IO
package com.jredu.iodemo; //Java 流(Stream)、文件(File)和IO import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class IOTest { public static void main(String[] args) { try { FileInputStream fis= new FileInputStream("G:/IO數據/io.text.txt");//調用文檔 //磁盤目錄下的文檔 byte[]data = new byte[1024]; int len; while((len=fis.read(data))!=-1){ String s=new String(data,0,len); System.out.println(s); } fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { FileOutputStream fos = new FileOutputStream("G:/IO數據/io.text.txt"); fos.write("abc".getBytes());//直接覆蓋裏面的內容 fos.flush(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
結果圖::
package com.jredu.iodemo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyDemo { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("G:/IO數據/123.png"); FileOutputStream fos = new FileOutputStream("G:/IO數據/1234.png"); byte []data = new byte[1024]; int len; 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 block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
結果圖:
Java學習記錄(補充八:Date類;Java流(Stream),文件(File)和IO)