跟个白痴似的,那个add(),会了。
就是一个日期加上若干时间,得出另一个日期会了。
但是如果是2个日期就中间所差的天数又遇到问题。。。
我是这么做的。但是不知道为什么得出来始终是1天,是方法用错了还是什么就不知道。
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class HM3
{
public static void main(String[] args)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd");
Date d = null;
Calendar c = new GregorianCalendar();
Calendar cc = new GregorianCalendar();
String fore = JOptionPane.showInputDialog(null,
"请输入被减日期(请以空格格开)");
String step = JOptionPane.showInputDialog(null,
"请输入最后日期(请以空格格开)");
try
{
d = df.parse(fore);
c.setTime(d);
}
catch(Exception e)
{
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,df.format(c.getTime()));
try
{
d = df.parse(step);
cc.setTime(d);
}
catch(Exception e)
{
e.printStackTrace();
}
JOptionPane.showMessageDialog(null,df.format(cc.getTime()));
JOptionPane.showMessageDialog(null," " +cc.compareTo(c));
}
}
[此贴子已经被作者于2007-3-2 22:22:44编辑过]
----------------解决方案--------------------------------------------------------
得到两个时间的这毫秒表示法,然后两个数相减就OK了
long t1=d.getTime();这个得到毫秒数,然后相减
----------------解决方案--------------------------------------------------------
没懂。
能告诉我 我原有的那个程序是该怎么改吗?
----------------解决方案--------------------------------------------------------
得到两个时间的毫秒表示法啊
调用两个Date对象的getTime()方法,得到一个long值,这个long值就是以毫秒表示的当时的时间
然后两个相减就可以了
我们知道一天的毫秒数是:24小时*60分钟*60秒*1000毫秒
然后两个毫秒数的差除以这个就知道相差几天了,你也可以知道相差几个小时,几分钟等等,只要换算成毫秒就行了
----------------解决方案--------------------------------------------------------
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class HM3
{
public static void main(String[] args)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd");
Date d = null;
Date dd = null;
Calendar c = new GregorianCalendar();
String fore = JOptionPane.showInputDialog(null,
"请输入被减日期(请以空格格开)");
String step = JOptionPane.showInputDialog(null,
"请输入最后日期(请以空格格开)");
try
{
d = df.parse(fore);
c.setTime(d);
}
catch(Exception e)
{
e.printStackTrace();
}
// JOptionPane.showMessageDialog(null,df.format(c.getTime()));
long t1 = d.getTime();
try
{
dd = df.parse(step);
c.setTime(dd);
}
catch(Exception e)
{
e.printStackTrace();
}
// JOptionPane.showMessageDialog(null,df.format(c.getTime()));
long t2 = dd.getTime();
JOptionPane.showMessageDialog(null,"相隔" + ((t2-t1)/(24*60*60*1000))+"天");
}
}
哈哈 出来了出来了..真是太感谢了!!
----------------解决方案--------------------------------------------------------
throws Exception 用那么多 try catch 干啥子 浪费代码
----------------解决方案--------------------------------------------------------
直接throws Exception 难道是让windows处理唢!!
----------------解决方案--------------------------------------------------------
谢谢了!~问题解决中……
----------------解决方案--------------------------------------------------------