I am trying to make a method where you can enter the arguments of, the current month and the amount of months you want to trace back, and convert all of this into days. For example, if the current month was January and I wanted convert the last 5 months into days the answer would be 153. I have the following information for one to use.
private Calendar cal;
private int currentMonth;
private final int JANUARY = 31;
private final int FEBRUARY = 28;
private final int MARCH = 31;
private final int APRIL = 30;
private final int MAY = 31;
private final int JUNE = 30;
private final int JULY = 31;
private final int AUGUST = 31;
private final int SEPTEMBER = 30;
private final int OCTOBER = 31;
private final int NOVEMBER = 30;
private final int DECEMBER = 31;
public Constructor(){
cal = new GregorianCalendar();
currentMonth = cal.get(Calendar.MONTH);
}
private int convertMonthsToDays(int currentMonth, int months){
int days;
return days;
}
解决方案
Use this code for what you want
package test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import com.ibm.icu.util.Calendar;
import com.ibm.icu.util.GregorianCalendar;
import com.mPower.fw.dao.ClassMaster;
import com.mPower.fw.impl.DaoServiceIMPL;
import com.mPower.fw.util.hibernateUtil;
public class runT {
public static void main(String[] args) throws IOException {
try{
Calendar calendar = new GregorianCalendar();
int CURRENT_MONTH = 0;
int MONTH_BACK = 5;
int totalDays = 0;
calendar.set(Calendar.MONTH, CURRENT_MONTH);
for(int i=0;i
totalDays += calendar.getMaximum(java.util.Calendar.DAY_OF_MONTH);
calendar.add(Calendar.MONTH, -1);
}
System.out.println(totalDays);
}catch(Exception e){
e.printStackTrace();
}
}
}
use calendar.add(Calendar.MONTH, -1); for Month back.
use calendar.add(Calendar.MONTH, 1); for month forward.