Saturday 1 May 2021

How to create folder with current timestamp in Java?


Let's first create a class called helper() and add below two methods to it"
- Create Folder

- fetch Current timestamp


public class helper(){

/*

* create folder

*/

public static void CreateFolder(String path)  {

//File is a class inside java.io package

File file = new File(path);

if (!file.exists()) {

file.mkdir();//mkdir is used to create folder

} else

System.out.println("Folder already created");

}

/*

* Return current time stamp

*/

public static String Timestamp() {

Date now = new Date();

String Timestamp = now.toString().replace(":", "-");

return Timestamp;

}

}


Now let's try to use the above two methods to create a folder in our project with timestamp:

 

Let's assume our project name is "TestProject" and we want to create the folder with a timestamp inside our /htmlReports which is inside our project:


String subfolderpath=System.getProperty("user.dir")+"/htmlReports/"+Helper.Timestamp(); //System.getProperty("user.dir") this will return project path and then we are appending folder name where we want to create the folder with timestamp

//create sub folder

Helper.CreateFolder(subfolderpath);

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

API Status Codes with examples for QA-Testers

  🔺 LinkedIn: https://www.linkedin.com/in/sidharth-shukla-77b53145/ 🔺 Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 🏮In API testing, it...