import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.text.*; public class TJOptionPane2 extends JApplet { Container container = null;
public void init() { // 1. Get sa handle on the applet's content pane. container = this.getContentPane();
// 2. Add a box to the content pane. Box box = new Box(BoxLayout.X_AXIS); container.add(box);
// 3. Add a button to the box. JButton button = new JButton("Show Time"); button.setPreferredSize(new Dimension(150,25)); button.addActionListener(new ButtonListener()); box.add(Box.createGlue()); box.add(button); box.add(Box.createGlue()); }
// 4. The listener class. class ButtonListener implements ActionListener { // 5. Argument values for the confirmation dialog box. Object confirmText = "Do You Wish To See Date Also?"; String confirmTitle = "Date Confirmation Dialog"; int optionType = JOptionPane.YES_NO_OPTION; int messageType1 = JOptionPane.QUESTION_MESSAGE;
// 6. Argument values for the message dialog box. Object information = null; String title = "Message Display Dialog"; int messageType2 = JOptionPane.INFORMATION_MESSAGE;
// 7. Option selected. // If the selection is 'yes', selectedValue = 0; // If the selection is 'No', selectedValue = 1; int selectedValue;
public void actionPerformed(ActionEvent e) { // 8. Display the confirmation dialog box. selectedValue = JOptionPane.showConfirmDialog(container, confirmText, confirmTitle, optionType, messageType1);
// 9. Fetch the time or date and time. information = fetchInformation();
// 10. Display the message. JOptionPane.showMessageDialog(container, information, title, messageType2); }
// 11. Returns the time or date and time depending // on the Yes or No choice made. public String fetchInformation() { DateFormat formatter = null;
if (selectedValue == 0) { //If it is Yes. formatter = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.LONG); } else if(selectedValue == 1) { //If it is No. formatter = DateFormat.getTimeInstance( DateFormat.LONG); }
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String c=sdf.format(current);
System.out.println(c);
}
}
public class Datetime { public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String c=sdf.format(current);
System.out.println(c);
}
}
又是作业?
//Listing 14.3 TJOptionPane2.java
/*
* <Applet code=TJOptionPane2 width=400 height=75>
* </Applet>
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*; public class TJOptionPane2 extends JApplet {
Container container = null;
public void init() {
// 1. Get sa handle on the applet's content pane.
container = this.getContentPane();
// 2. Add a box to the content pane.
Box box = new Box(BoxLayout.X_AXIS);
container.add(box);
// 3. Add a button to the box.
JButton button = new JButton("Show Time");
button.setPreferredSize(new Dimension(150,25));
button.addActionListener(new ButtonListener());
box.add(Box.createGlue());
box.add(button);
box.add(Box.createGlue());
}
// 4. The listener class.
class ButtonListener implements ActionListener {
// 5. Argument values for the confirmation dialog box.
Object confirmText = "Do You Wish To See Date Also?";
String confirmTitle = "Date Confirmation Dialog";
int optionType = JOptionPane.YES_NO_OPTION;
int messageType1 = JOptionPane.QUESTION_MESSAGE;
// 6. Argument values for the message dialog box.
Object information = null;
String title = "Message Display Dialog";
int messageType2 = JOptionPane.INFORMATION_MESSAGE;
// 7. Option selected.
// If the selection is 'yes', selectedValue = 0;
// If the selection is 'No', selectedValue = 1;
int selectedValue;
public void actionPerformed(ActionEvent e) {
// 8. Display the confirmation dialog box.
selectedValue = JOptionPane.showConfirmDialog(container,
confirmText, confirmTitle,
optionType, messageType1);
// 9. Fetch the time or date and time.
information = fetchInformation();
// 10. Display the message.
JOptionPane.showMessageDialog(container,
information, title,
messageType2);
}
// 11. Returns the time or date and time depending
// on the Yes or No choice made.
public String fetchInformation() {
DateFormat formatter = null;
if (selectedValue == 0) { //If it is Yes.
formatter = DateFormat.getDateTimeInstance(
DateFormat.SHORT,
DateFormat.LONG);
}
else if(selectedValue == 1) { //If it is No.
formatter = DateFormat.getTimeInstance(
DateFormat.LONG);
}
// Format the time or date and time and return.
return(formatter.format(new Date()));
}
}
}
太不好意思了,这个是另一个帖子的代码。
他问如何执行这个代码
http://topic.csdn.net/u/20081112/14/a0f126c3-5e4a-49ae-84fb-e98906f09cbe.html
public class Datetime { public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String c=sdf.format(current);
System.out.println(c);
}
} public class Datetime { public static void main(String args[]){
java.util.Date current=new java.util.Date();
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String c=sdf.format(current);
System.out.println(c);
}
}
import java.awt.*;
import java.util.*;
import java.text.*;
import java.applet.*;public class ShowTime extends Applet
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = df.format(new Date());
Label la = new Label();
public void init()
{
setLayout(new FlowLayout());
la.setText(s);
add(la);
}
}
import java.util.Calendar;
import java.util.Date;
import java.util.SimpleDateFormat;
public class DateExamples{
public static void main(String[] args){
Calendar thisInstant=Calendar.getInstance();
Date rightNow=thisInstant.getTime();
String ex1=rightNow.toString();
System.out.println("Example 1:");
System.out.println("Current time and date:"+ex1);
System.out.println();
Date now=new Date(System.currentTimeMillis());
SimpleDateFormat sdfEx2=new SimpleDateFormat("EEEE,MMM,d,yyyy hh:mm:aaa");
String ex2=sdfEx2.format(now);
System.out.println("Example 2");
System.out.println("Current time and date:"+ex2);
System.out.println();
SimpleDateFormat sdfEx3=new SimpleDateFormat("MMMM,d,yyyy");
String ex3=sdfEx3.format(now);
System.out.println("Example 3");
System.out.println("Current time and date:"+ex3);
System.out.println();
SimpleDateFormat sdfEx4=new SimpleDateFormat("MM/dd/yyyy");
String ex4=sdfEx4.format(now);
System.out.println("Example 4");
System.out.println("Current time and date:"+ex4);
System.out.println();
Calendar cal=Calendar.getInstance();
cal.set(2008,Calendar.NOVEMBER,17);
Date specialDate=cal.getTime();
String ex5=sdfEx4.format(specialDate);
System.out.println("Example 5");
System.out.println("Current time and date:"+ex5);
System.out.println();
}
}是不是LZ想要的?