Name a file in Java to include date and time stamp

前端 未结 3 1912
轮回少年
轮回少年 2020-12-12 07:31

I am exporting data into a file in a Java application using NetBeans. The file will have a hard coded name given by me in the code. Please find below the code.



        
3条回答
  •  情深已故
    2020-12-12 07:49

    You can do something like that

    Define your file name pattern as below :

    private static final String FILE = "D:\Report_{0}.pdf";
    private static final String DATE_PATTERN = "yyyy-MM-dd:HH:mm:ss";
    

    The {0} is a marker to be replaced by the following code

    private String formatFileName(Date timeStamp) {
            DateFormat dateFormatter = new SimpleDateFormat(DATE_PATTERN);
            String dateStr = dateFormatter.format(timeStamp);
            return MessageFormat.format(FILE, dateStr); 
    }
    

    For further information over date format you can see there and MessageFormat there

提交回复
热议问题