View Full Version : How do I print out current date and time in Java?

Sep 28, 2008, 09:30 AM

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.

Sep 28, 2008, 10: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.

Sep 28, 2008, 02:35 PM
Thank you for your help!
appreciated. :)

Sep 28, 2008, 07: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.