In PHP if you do:
$date = "2013-08-31";
$nextdate = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = date( "Y-m-d", $nextdate );
echo $date;
You get 2013-10-01 ie the month has rolled over since there are only 30 days in September.
In MySQL if you do:
UPDATE member_account SET NextBillDate = '2013-08-31'
SELECT DATE_ADD(NextBillDate, INTERVAL 1 MONTH) FROM member_account
You get 2013-09-30 ie no roll-over
In Java it's the same thing:
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 is "2013-09-31"
Is there a way of making MySQL or Java behave the same way as PHP so it will rollover if the number of days in the month is exceeded?
Found this little Java snippet, does the trick:
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() );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.