奇怪的错误

haiye 2007-05-13 11:45:21
这个类在编译时没有问题,在运行时得到的结果如下:

输入一个日期:
输入年份:2007
输入月份:1
输入日期:31
2007年2月3日
输入另一个日期:
输入年份:2007
输入月份:12
输入日期:31
2008年0月31日
你输入的两个日期相隔334

正确的应该是:

输入一个日期:
输入年份:2007
输入月份:1
输入日期:31
2007年1月31日
输入另一个日期:
输入年份:2007
输入月份:12
输入日期:31
2007年12月31日
你输入的两个日期相隔334


只要输入带31日的月份,都会出错。

源代码在1.6的环境中运行:

//此类要用jdk1.5.0以上

import java.util.*;
public class OnlyDayInstance {

/**
* @param args
*/
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
Scanner scanner = new Scanner(System.in);
int year = 0 ;
int month = 0 ;
int day = 0 ;
int otheryear = 0 ;
int othermonth = 0 ;
int otherday = 0 ;
System.out.println("输入一个日期:");
System.out.print("输入年份:");
year = scanner.nextInt();
System.out.print("输入月份:");
month = scanner.nextInt();
System.out.print("输入日期:");
day = scanner.nextInt();

calendar.set(year, month, day);
System.out.println(calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DATE)+"日");
long time1 = calendar.getTimeInMillis();

System.out.println("输入另一个日期:");
System.out.print("输入年份:");
otheryear = scanner.nextInt();
System.out.print("输入月份:");
othermonth = scanner.nextInt();
System.out.print("输入日期:");
otherday = scanner.nextInt();
calendar.set(otheryear, othermonth, otherday);
System.out.println(calendar.get(Calendar.YEAR)+"年"+calendar.get(Calendar.MONTH)+"月"+calendar.get(Calendar.DATE)+"日");
long time2 = calendar.getTimeInMillis();
long distime = (time2 - time1) / (1000*60*60*24);
System.out.println("你输入的两个日期相隔"+distime);
}

}


















...全文
301 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
邬小洪 2007-05-14
  • 打赏
  • 举报
回复
我想抓异常用什么类呢?
haiye 2007-05-14
  • 打赏
  • 举报
回复
问题解决了,谢谢各位拉!马上散分!
邬小洪 2007-05-14
  • 打赏
  • 举报
回复
呵呵
谢谢!
z_lping 2007-05-14
  • 打赏
  • 举报
回复
你改了一个,就不会改俩吗?
z_lping 2007-05-14
  • 打赏
  • 举报
回复
import java.util.*;
public class OnlyDayInstance {

/**
* @param args
*/
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
Scanner scanner = new Scanner(System.in);
int year = 0 ;
int month = 0 ;
int day = 0 ;
int otheryear = 0 ;
int othermonth = 0 ;
int otherday = 0 ;
System.out.println("输入一个日期:");
System.out.print("输入年份:");
year = scanner.nextInt();
System.out.print("输入月份:");
month = scanner.nextInt();
System.out.print("输入日期:");
day = scanner.nextInt();

calendar.set(year, month-1, day);
System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");
long time1 = calendar.getTimeInMillis();

System.out.println("输入另一个日期:");
System.out.print("输入年份:");
otheryear = scanner.nextInt();
System.out.print("输入月份:");
othermonth = scanner.nextInt();
System.out.print("输入日期:");
otherday = scanner.nextInt();

calendar.set(otheryear, othermonth-1, otherday);
System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");
long time2 = calendar.getTimeInMillis();
long distime = (time2 - time1) / (1000*60*60*24);
System.out.println("你输入的两个日期相隔"+distime);
}

}
邬小洪 2007-05-14
  • 打赏
  • 举报
回复
输入一个日期:
输入年份:2007
输入月份:1
输入日期:31
2007年0月31日
输入另一个日期:
输入年份:2007
输入月份:12
输入日期:31
2007年11月31日
你输入的两个日期相隔334

如果按照 z_lping(Schemer) 说的做的话就会出现上面的结果!
还是不对!
haiye 2007-05-14
  • 打赏
  • 举报
回复
z_lping(Schemer) 能给调试一下吗?
haiye 2007-05-14
  • 打赏
  • 举报
回复
我试了 z_lping(Schemer) 的
calendar.set(year, month-1, day);
System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");

还是不行.

qfs_v 2007-05-14
  • 打赏
  • 举报
回复
输入月份:1
2007年2月3日
......
输入月份:12
2008年0月31日

很明显相差1个月.
z_lping 2007-05-14
  • 打赏
  • 举报
回复
calendar.set(year, month-1, day);
System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日");
haiye 2007-05-14
  • 打赏
  • 举报
回复
都试过了,还是不行。
qfs_v 2007-05-14
  • 打赏
  • 举报
回复
月是从0~11滴.
z_lping 2007-05-14
  • 打赏
  • 举报
回复
该减的时候减,改加的时候加。
haiye 2007-05-14
  • 打赏
  • 举报
回复
我试过了,加1还是不行啊!
z_lping 2007-05-13
  • 打赏
  • 举报
回复
读API不仔细啊,Calender里月份是从0到11的,在处理的时候需要减一加一的。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧