java 生成date_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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值