mastercool
mastercool

Reputation: 523

How to use a String with Date to create a new File in Java

Why am I able to create a directory like this: new File("./HISTORY/www").mkdirs(); But if I try to use a String, then nothing happens:

    String path = "./HISTORY/" + history_folder_name;
        new File(path).mkdirs();

This does not create the directory like the other one does. Why? EDIT: this is how I am creating the folder name:

         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
         Date now = new Date();
         String strDate = sdf.format(now);
         history_folder_name = "folderName";

It works fine if i take out the HH:mm:ss:SSS but I need them. What solution is there?

Upvotes: 0

Views: 669

Answers (2)

shl_droco
shl_droco

Reputation: 94

Change the

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

to

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS");

Upvotes: 1

Petronella
Petronella

Reputation: 2545

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
     Date now = new Date();
     String strDate = sdf.format(now).replace(":", ".").replace("-", "_");

Upvotes: 1

Related Questions