Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create folder using DateTime as name

Tags:

java

file

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();

    }
like image 851
Nick Avatar asked Jan 14 '16 23:01

Nick


People also ask

How do I create a directory with today's date?

The command is date +%Y%m%d so that a directory for today would be 20060811.

How do I create a timestamp in a folder?

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.

What is a time stamped directory?

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.


1 Answers

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 -.

like image 50
mech Avatar answered Sep 22 '22 14:09

mech