java.util.Date da = new java.util.Date();
System.out.println(fm.format(da.getTime()));
我只知道这样格式现在的日期,但是不知道怎么格式输入的日期。。
import javax.swing.*;
import java.util.*;
import java.text.*;
public class HW2
{
public static void main(String[] args)
{
GregorianCalendar gc = new GregorianCalendar();
String input=JOptionPane.showInputDialog(null,"请输入日期:(格式:yyyy-MM-dd)");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
java.util.Date da = new java.util.Date();
Date now=sdf.parse(input);
int count = JOptionPane.showInternalConfirmDialog(null,"请输入您想计算的天数");
}
}
就是那句话要报错
[此贴子已经被作者于2007-2-28 22:11:36编辑过]
[此贴子已经被作者于2007-2-28 23:39:18编辑过]
下面代码体现了我上面提出的二个问题..
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.text.DateFormat;
public class DateTest extends JFrame
{
DateTest(String title)
{
super(title);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(new MyPanel());
}
public static void main(String[] args)
{
DateTest dt = new DateTest("HelloWorld");
dt.setSize(new Dimension(300,300));
dt.setVisible(true);
}
}
class MyPanel extends JPanel
{
JButton btnInit = new JButton("设定初始日期");
JButton btnVeiw = new JButton("预看N天后日期");
JButton btnSet = new JButton("设为N天后日期");
String date="";
Calendar c = new GregorianCalendar(); //对同一个时间对象操作
DateFormat df = DateFormat.getDateInstance();
Date d = null;
MyPanel()
{
this.setLayout(new BorderLayout());
this.add(btnInit,BorderLayout.NORTH);
this.add(btnVeiw,BorderLayout.CENTER);
this.add(btnSet,BorderLayout.SOUTH);
//设定初始日期
btnInit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
date = JOptionPane.showInputDialog("请输入一个日期:(YYYY-MM-DD)");
try {
d = df.parse(date);
c.setTime(d);
} catch (ParseException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
});
//预看N天后日期
btnVeiw.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int addDate = Integer.parseInt(JOptionPane.showInputDialog("请输入希望得到几天后的日期:"));
c.add(Calendar.DAY_OF_MONTH,addDate);
DateFormat dff = DateFormat.getDateInstance(DateFormat.FULL);
JOptionPane.showMessageDialog(null, "日期是:" + dff.format(c.getTime()));
}
});
btnSet.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int addDate = Integer.parseInt(JOptionPane.showInputDialog("请输入希望得到几天后的日期:"));
c.set(Calendar.DATE,addDate);
DateFormat dff = DateFormat.getDateInstance(DateFormat.FULL);
JOptionPane.showMessageDialog(null, "日期是:" + dff.format(c.getTime()));
}
});
}
}