Topic: java.sql.date 的日期计算问题

  Print this page

1.java.sql.date 的日期计算问题 Copy to clipboard
Posted by: jerry010
Posted on: 2004-09-05 22:05

通过java.sql.date可以计算日期的,可是遇到一个问题:
就是8月31日等几个月末使用java.sql.date(date.getYear,date.getMonth+1,date.getDate)得出月份的结果是下两个月的返回值,如2004/08/31-->2004/10
何故啊?如何解决?谢谢

2.Re:java.sql.date 的日期计算问题 [Re: jerry010] Copy to clipboard
Posted by: why
Posted on: 2004-09-05 22:33

1. java.sql.Date(int year, int month, int day) is deprecated.
use java.util.Calendar

2. 2004/08/31 --(month + 1)--> 2004/09/31
but 2004/09 does not have the 31st day, so the result is 2004/10/01

3.Re:java.sql.date 的日期计算问题 [Re: jerry010] Copy to clipboard
Posted by: why
Posted on: 2004-09-06 08:28

java.sql.Date(date.getYear(),date.getMonth()+1,date.getDate())

you don't have to add 1 to date,getMonth()
since both java.sql.Date(int,int,int) and java.util.Date(int,int,int) expects the month value from 0 to 11

check http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Date.html#Date(int,%20int,%20int)
and http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html#Date(int,%20int,%20int)


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923