標籤:java;檔案名稱自動產生;
1.Java中動態產生當前日期的檔案名稱並且將控制台的輸出資訊輸入到檔案中 public static void SaveClonseToFile() throws IOException, FileNotFoundException { File f = new File(getCurrentDateFileName() + ".txt"); f.createNewFile(); FileOutputStream fileOutputStream = new FileOutputStream(f); PrintStream printStream = new PrintStream(fileOutputStream); System.setOut(printStream); //將控制台資訊輸出到檔案中 } public static String getCurrentDateFileName() {SimpleDateFormat simpleDateFormat;simpleDateFormat = new SimpleDateFormat("yyyyMMdd");Date date = new Date();String str = simpleDateFormat.format(date);return str; // 目前時間 }2.產生當前日期加隨機的數的字串用於組建檔案名 public static String getRandomFileName() { SimpleDateFormat simpleDateFormat;simpleDateFormat = new SimpleDateFormat("yyyyMMdd");Date date = new Date();String str = simpleDateFormat.format(date); Random random = new Random(); int num = (int) (random.nexInt()*100+1);return str+num; // 目前時間}3.判斷指定的日期是星期幾 public static int dayForWeek(String pTime) throws Exception {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");Calendar c = Calendar.getInstance();c.setTime(format.parse(pTime));int dayOfWeek = 0;if (c.get(Calendar.DAY_OF_WEEK) == 1) {dayOfWeek = 7;} else {dayOfWeek = c.get(Calendar.DAY_OF_WEEK) - 1;}return dayOfWeek;}
本文出自 “Apple” 部落格,請務必保留此出處http://59465168.blog.51cto.com/5268021/1965751
Java中動態產生當前日期的檔案