I am trying to create a folder using a TimeStamp as the folder name. The code I am using will not create the folder when I use the timeStamp variable in the code below. However if I set the folder name directly like this ...
File dir = new File("Hello")
The folder is created. Is this the right way to set a folder name using Date and time?
public void logEmData(String reason,Campus c ) throws IOException
{
LocalDateTime time = LocalDateTime.now();
try(FileWriter writer = new FileWriter(file, true))
{
writer.write("Building " + c.getName() + " Entered Emergency Mode" + System.lineSeparator());
writer.write(" Reason: " + reason + System.lineSeparator());
writer.write(time.toString() + System.lineSeparator());
//Create folder
String timeStamp = "EM_" + time;
File dir = new File(timeStamp);
dir.mkdir();
}
The command is date +%Y%m%d so that a directory for today would be 20060811.
Step 1: Firstly, navigate to the parent folder where you want to create the folder and name it based on the system's current timestamp. As next, right click on an empty space, click on New and then click on the Text Document option. Step 2: Now double click on the newly created text document to edit it.
These allow you to determine when a file or folder was created, when it was last updated and the time that it was most recently accessed.
LocalDateTime.now()
gets you a date with a format roughly like so: 2015-01-14T16:00:49.455
.
:
is a problematic character for folder creation under Windows due to it being a reserved character. You may want to consider formatting the string to change it to -
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With