I want to add date to filename just before the file extension.
For example
Input : test.txt or test.png
Output : test-02-Feb-2018_13:05:17-am.txt or test-02-Feb-2018_13:05:17-am.png
Note: anytype not txt only
I want to add date to filename just before the file extension.
For example
Input : test.txt or test.png
Output : test-02-Feb-2018_13:05:17-am.txt or test-02-Feb-2018_13:05:17-am.png
Note: anytype not txt only
You should extract base file name and extension from the file, and then reconstruct the file name by adding the date.
This two links should help you :
How do I get the file extension of a file in Java?
How to get the filename without the extension in Java?
I suppose you already know how to format a date
The easiest option is (assuming you always have an extension) is to get the last index of .
and use String::substring
to get the parts of the filename you need, and insert the timestamp of choice.
For example, something you could use and manipulate to your liking:
String filename = "test.txt";
System.out.println(filename.substring(0, filename.lastIndexOf(".")) + LocalDateTime.now() + filename.substring(filename.lastIndexOf(".")));
Output:
test2019-01-21T15:20:34.992.txt