在PHP中,如果这样做:

$date = "2013-08-31";
$nextdate = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = date( "Y-m-d", $nextdate );
echo $date;

你会得到2013-10-01,也就是说,这个月已经过去了,因为9月份只有30天。
在MySQL中,如果这样做:
UPDATE member_account SET NextBillDate = '2013-08-31'
SELECT DATE_ADD(NextBillDate, INTERVAL 1 MONTH) FROM member_account

2013年9月30日,即无翻滚
在Java中也是一样:
GregorianCalendar oDate = new GregorianCalendar();
SimpleDateFormat Sdf = new SimpleDateFormat( "yyyy-MM-dd" );
try{oDate.setTime(Sdf.parse("2013-08-31"));} catch (Exception e) {}
String sTodayDate = Sdf.format( oDate.getTime() );

oDate.add(Calendar.MONTH, 1);
String sTodayDate2 = Sdf.format( oDate.getTime() );

sTodayDate2是“2013-09-31”
是否有办法使MySQL或Java的行为与PHP相同,以便在超过一个月的天数时滚动?

最佳答案

找到这个小Java片段,就可以做到:

    int oldDay = oDate.get(Calendar.DAY_OF_MONTH);
    oDate.add(Calendar.MONTH, 1);

    // If the old DAY_OF_MONTH was larger than our new one, then
    // roll over to the beginning of the next month.
    if (oldDay > oDate.get(Calendar.DAY_OF_MONTH)){
        oDate.add(Calendar.MONTH, 1);
        oDate.set(Calendar.DATE, 1);
    }

    String sTodayDate2 = Sdf.format( oDate.getTime() );

10-05 19:48