Title: Convert a set of months into days using Java
Tags: java;helper
Question: 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;
Comment: Is there a calendar method that I could use..? Should I use a loop? I am not sure how to do it.

Here is the accepted answer: 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<MONTH_BACK;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.
Comment for this answer: i dont see the need for a loop to solve this kind of problem, i like @Eric R.'s solution much better calculating the difference in millis and converting it back to days. much faster and cleaner IMHO

Here is another answer: Why making your own algorithm when everything is in Java.
Just calculate the millisecond beetween now and 5 month before.
long now = System.currentTimeMillis();
Calendar calendar = GregorianCalendar.getInstance();
//Here I set the calendar to now. But you can set the date you want.
// by using the method  calendar.set()
calendar.setTimeInMillis(now);
calendar.add(Calendar.MONTH, -5);
long previous = calendar.getTimeInMillis();
int numberOfDays =(int)( (now - previous) / (1000* 3600 *24));
Hope it's help.
Regards.

Here is another answer: The primary problem with this implementation is that it's pinned to 2000, rather than the current year.
private int convertMonthsToDays(int currentMonth, int months){
int days = 0;
for (int iter=0; iter < months; iter++)
currentMonth = (currentMonth - iter) % 12;
this.cal.set(2000, currentMonth, 1);
days = days + this.cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return days;
Comment for this answer: you called the final array `months` and took in `months` as an int. You'll have to change one of them.

Here is another answer: I'm not sure if you are allowed to use SimpleDateFormat however here is an example that's pretty easy IMO. Don't know how the community feels about it but it works:
private static SimpleDateFormat monthNumberFormat = new SimpleDateFormat("M");
private static SimpleDateFormat daysOfYearFormat = new SimpleDateFormat("DDD");
private static int convertMonthsToDays(int currentMonth, int months){
try {
Date startingDate = monthNumberFormat.parse(currentMonth + "");
Date endingDate = monthNumberFormat.parse(currentMonth - months + "");
return Integer.parseInt(daysOfYearFormat.format(startingDate)) -
Integer.parseInt(daysOfYearFormat.format(endingDate));
} catch (ParseException e) {
return -1;
} catch (NumberFormatException nfe){
return -1;
There is no checking here to see if you are passing valid parameters and it also does not account for going back over a year.
Using this method you can pass the number of the desired month and the number of months to go back and it will give you the difference in days.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:04/10/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><helper>', 'Title': 'Convert a set of months into days using Java', 'ViewCount': 5121, 'id': 10082186, 'user_AboutMe': '', 'user_AccountId': 1298465.0, 'user_DisplayName': 'Trey', 'user_Reputation': 151.0, 'user_Views': 19.0, 'user_id': 1248873.0}}