I have a program that displays info based on the day. Everything has b开发者_JAVA百科een working fine up till today. for some reason when have the calendar roll back one day to get yesterdays date it goes straight to august 30th. it skips august 31st. It will grab todays date fine (septermber 1st) but when i trace it, it goes right past the 31st. anyone suggestions on what I'm doing wrong or a better way to do it? here is my code
Calendar date2;
date2 = Calendar.getInstance();
mday1 = date2.get(Calendar.DATE);
date2.roll(Calendar.DATE, false); //rolls back the date by one day
mday2 = date2.get(Calendar.DATE);
It doesn't go to previous month. It stays in the same month (check the month field yourself). It just changes the day of the month. You'd like to use Calendar#add()
instead with a negative value.
Calendar date2;
date2 = Calendar.getInstance();
mday1 = date2.get(Calendar.DATE);
date2.add(Calendar.DATE, -1); //rolls back the date by one day
mday2 = date2.get(Calendar.DATE);
精彩评论