Trey
Trey

Reputation: 171

Convert a set of months into days using Java

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;
}

Upvotes: 1

Views: 6429

Answers (4)

nattyddubbs
nattyddubbs

Reputation: 2095

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.

Upvotes: 0

Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

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.

Upvotes: 2

user973999
user973999

Reputation:

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.

Upvotes: 2

Nathaniel Ford
Nathaniel Ford

Reputation: 21239

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;
}

Upvotes: 0

Related Questions