New to java, need to know how to incorparate system time into a block of code.
Hello, below is a simple block of code. I have been trying to add a println of the
system time. I tried many examples but most start with import java.util.Calendar;
This gets an error in eclipse stating this is never used. I tried many examples and
get the same error with different import calls. Can someone add to my code
example the minimum code to print out the time?
Code
Code Java:
package com.allmycode.first;
public class GoodbyeMoon {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String var1 = "GoodbyeMoon";
int var2 = 10;
System.out.println(var1+" "+ var2);
String var3 = "Done";
System.out.println(var3);
}
}
Re: New to java, need to know how to incorparate system time into a block of code.
That's not an error, it's just a warning. It's telling you that you have an import that you don't use. I don't see anything in your code that attempts to print out the time. Check out the System API for some useful functions: System (Java Platform SE 6)
Re: New to java, need to know how to incorparate system time into a block of code.
What format do you want to time printed in?
There are several different classes or methods that could be used.
Re: New to java, need to know how to incorparate system time into a block of code.
Quote:
Originally Posted by
Norm
What format do you want to time printed in?
There are several different classes or methods that could be used.
I would like to see the time as hour min example 2:20
Thanks
Re: New to java, need to know how to incorparate system time into a block of code.
Look at the DateFormat class.
Re: New to java, need to know how to incorparate system time into a block of code.
Norm, thank you; I found a way to do it see below, not prity but it worked. The trick was the import string I tried did not work.
package com.allmycode.first;
import java.util.Date;
public class GoodbyeMoon {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String var1 = "GoodbyeMoon";
int var2 = 10;
Date date = new Date();
System.out.println("Today is " + date);
System.out.println(var1+" "+ var2);
String var3 = "Done";
System.out.println(var3);
}
}