Getting Time & Date

Can anyone have a look at this and tell me why it is not working - it only prints out the first try statement block to the console... :(

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

publicclass DateTime{

public DateTime()

{

}

publicstaticvoid main ( String [] args )

{

Runtime r = Runtime.getRuntime();

try{

Process p0 = r.exec(command0);

br =new BufferedReader(new InputStreamReader(p0.getInputStream()));

currentLine = br.readLine();

while(currentLine !=null){

System.out.println(currentLine);

currentLine = br.readLine() ;

}

}catch (IOException ioe){

ioe.printStackTrace();

}

try{

Process p1 = r.exec(command1);

br =new BufferedReader(new InputStreamReader(p1.getInputStream()));

currentLine = br.readLine();

while(currentLine !=null){

System.out.println(currentLine);

currentLine = br.readLine() ;

}

}catch (IOException ioe){

ioe.printStackTrace();

}

}

static String currentLine;

static BufferedReader br;

static String command0 ="cmd /C Time";

static String command1 ="cmd /C Date";

}

[2633 byte] By [Java.Petea] at [2007-11-27 2:57:23]
# 1
What exactly does it print? The Time function takes input, so it may be that the process is blocking waiting for that input, and your code is blocked waiting for the process' output. Kind of like a deadlock.
hunter9000a at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 2

The" /C" part of the command stops the input from being needed. So the command only gives output.

At the moment all I get is this from the console:

The current time is: 14:35:46.72

What i need to get is

The current time is: 14:35:46.72

The current date is: 01/05/2007

Java.Petea at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 3

> The" /C" part of the command stops the input from

> being needed. So the command only gives output.

Not on my system it doesn't.

> At the moment all I get is this from the console:

>

> > The current time is: 14:35:46.72

>

>

> What i need to get is

>

> > The current time is: 14:35:46.72

> The current date is: 01/05/2007

>

When I run that command, it prints the time, then asks for the new time:

The current time is: 10:04:43.13

Enter the new time:_

When I use /C, it gives an error:

'/C' is not recognized as an internal or external command,

operable program or batch file.

hunter9000a at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 4
what system are you runing it on?Im running on Xp Home.I believe for other XP pro and 2000 the command itcommand Time /Tcommand Date /T
Java.Petea at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 5

Hey, thanks for your help!

i've hacked away at it and I've got it to work :)

static String command0 = "cmd /C Time /T";

static String command1 = "cmd /C Date /T";

That works :)

Java.Petea at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 6
> That works :)That's nice. But it's a horribly convoluted bit of code that could be replaced by something simple using "new Date()" and a SimpleDateFormat object.
DrClapa at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...
# 7
Talk about going out of your way to make code platform dependent!
DrLaszloJamfa at 2007-7-12 3:35:51 > top of Java-index,Java Essentials,New To Java...