View Full Version : How do I print out current date and time in Java?
iSamurai
Sep 28, 2008, 10:30 AM
Hello,
Can someone please help me with printing out the current date and time. It just has to be a simple, one line, date/time in JOptionPane.
I'd like to see the entire java file -- from the import xxx, to the JOptionPane.showMessageDialog line...
Thank you.
Wes
Sep 28, 2008, 11:22 AM
import java.util.Date;
import javax.swing.JOptionPane;
public class DateTime {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, new Date());
}
}
This is relying on the toString() method of the Date class. Declaring a new Date object with no parameters creates a Date instance with the current date/time.
The null in the call is there as the method optionally takes the parent component of this dialog box.
iSamurai
Sep 28, 2008, 03:35 PM
Thank you for your help!
appreciated. :)
foidulus
Sep 28, 2008, 08:09 PM
Just to expound upon what Wes said, you might want to check out the SimpleDateFormatter class if don't like the default format, pretty easy to use and very handy.
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.