- 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;
- }
来源: http://www.bubuko.com/infodetail-2310602.html