使用getDate取出的年和月为啥不正确,先谢谢

xkwth 2004-11-02 11:11:43
环境:win2k+tomcat5014+j2se142
我在jsp中获取时间出现了问题,这个是怎么回事
<%
java.util.Date myDate=new java.util.Date();
int year=myDate.getYear();
int month=myDate.getMonth();
int day=myDate.getDay();
int hours=myDate.getHours();
int minutes=myDate.getMinutes();
int seconds=myDate.getSeconds();
String Month=month+"";
if(Month.length()<2) Month="0"+Month;
String Day=day+"";
if(Day.length()<2) Day="0"+Day;
String Hours=hours+"";
if(Hours.length()<2) Hours="0"+Hours;
String Minutes=minutes+"";
if(Minutes.length()<2) Minutes="0"+Minutes;
String Seconds=seconds+"";
if(Seconds.length()<2) Seconds="0"+Seconds;

String locale_Date=year+"-"+Month+"-"+Day+" "+Hours+":"+Minutes+":"+Seconds;
out.print(locale_date);
%>
显示的结果为:104-10-02 10:57:04
今天应当是2004年11月啊,迷惑,如何整改,先谢谢!!!
...全文
211 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
trumplet 2004-11-02
  • 打赏
  • 举报
回复
java.util.Date.getYear():
getYear
public int getYear()Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

Returns:
the year represented by this date, minus 1900.
See Also:
Calendar

根据API里的这一段,可以看出,你取出的年是104,加上1900,正好是2004,没错。

getMonth
public int getMonth()Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.MONTH).

Returns a number representing the month that contains or begins with the instant in time represented by this Date object. The value returned is between 0 and 11, with the value 0 representing January.

Returns:
the month represented by this date.
See Also:
Calendar

根据API里的这一段,可以看出,你取出的月是10,加上1,正好是11,没错。
xinfeng802 2004-11-02
  • 打赏
  • 举报
回复
int Date1,Year,Month;
long time;
java.util.Date today;
today =new java.util.Date() ;
Year=1900+today.getYear();
Date1=today.getDate();
Month=1+today.getMonth();
date1=Year+"年"+Month+"月"+Date1+"日";
time=today.getTime();
good_good 2004-11-02
  • 打赏
  • 举报
回复
date 的类取得的月要加1。给分吧最好用日历的类calendar

81,122

社区成员

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

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