1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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;
}
|
本文转自爱情89757 51CTO博客,原文链接:http://blog.51cto.com/59465168/1965751
,如需转载请自行联系原作者