Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iSamurai

macrumors 65816
Original poster
Nov 9, 2007
1,024
6
ɹǝpun uʍop 'ǝuɐqsı&#
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.
 

4409723

Suspended
Jun 22, 2001
2,221
0
Code:
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.
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.