简体   繁体   English

使用 Java 将一组月数转换为天数

[英]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.我正在尝试制作一种方法,您可以在其中输入当前月份的 arguments 和您要追溯的月份数量,并将所有这些转换为天数。 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.例如,如果当前月份是 1 月,我想将过去 5 个月转换为天数,则答案为 153。我有以下信息可供使用。

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

Why making your own algorithm when everything is in Java.当一切都在 Java 中时,为什么要制作自己的算法。

Just calculate the millisecond beetween now and 5 month before.只需计算现在和 5 个月前之间的毫秒数。

    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.问候。

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 );使用 calendar.add(Calendar.MONTH, -1 ); for Month back.一个月回来。

use calendar.add(Calendar.MONTH, 1 );使用 calendar.add(Calendar.MONTH, 1 ); for month forward.一个月前。

The primary problem with this implementation is that it's pinned to 2000, rather than the current year.此实现的主要问题是它固定到 2000 年,而不是当前年份。

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

I'm not sure if you are allowed to use SimpleDateFormat however here is an example that's pretty easy IMO.我不确定您是否被允许使用 SimpleDateFormat 但是这里是一个非常简单的 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.使用此方法,您可以将所需的月份数和月份数传回 go,它会给您天数差值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM