Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I need a code which should get Date from Excel as YYYY-MM-DD.

It is displaying the date as 31-Dec-2050 which was originally saved in a sheet as 2050-12-31 Format.

System.out.println("<" + sheet.getRow(i).getCell(24) + ">");

should get Date from Excel as YYYY-MM-DD.

2 Answers

0 votes
by (62.9k points)

Import the following libraries:

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

Then in your class, execute the following functions:

String cv = sheet.getRow(i).getCell(24).getStringCellValue();

DateTimeFormatter dt = DateTimeFormatter.ofPattern( "uuuu-MM-dd");

System.out.println("<" + LocalDate.parse(cv).format(dt) + ">");

0 votes
by (1.7k points)

To run the commands in a specific directory in Java, you are required to set the working directory or navigate to it through the command you send to cmd.exe. The Runtime.getRuntime().exec() function executes a sequence of commands found in the Command Prompt, navigating to a directory and further executing other commands.

Following is the guide and an altered version of your code to accomplish this:

Major Steps:

1. Open Command Prompt and change (cd) to given directory

Run in batch mode, the commands separated with && to ensure that if the previous command was successful then only the next one will be executed.

End

// Execute several commands in a chain

// "cd" to the desired directory, then execute more commands

String[] commands = { "cmd.exe", "/c", "cd " + newDir + " && dir" };

// Run the command

Process process = rt.exec(commands);

// You can read the output of the process if needed (optional)

process.getInputStream().transferTo(System.out);

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

CmdRunner cmdRunner = new CmdRunner();

// Change to your desired directory path

cmdRunner.excCommand("C:\\Program Files\\Flowella");

}

}


 

Explanation

1. cmd.exe /c: Opens the Command Prompt and runs commands until it exits.

2. cd <directory> && dir:

- cd <directory> navigates to the specified directory.

- && dir lists files in the directory. You just replace dir with whatever operation you wish to carry on that directory.

3. Pipeline Output (Optional)

The line process.getInputStream().transferTo(System.out); catches the output of any commands you run and it will print it on to your console.

Running In Sequence

Should you wish to do lots of different operations on in the same console window all at once, you might want to group commands so that you type:


 

String[] commands = { "cmd.exe", "\\""/c\\"", "cd " + newDir + " && your_command_here && another_command" };

Related questions

0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)
0 votes
2 answers
0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...