使用Java将几个月的时间转换成几天的时间 [英] Convert a set of months into days using Java

查看:437
本文介绍了使用Java将几个月的时间转换成几天的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一种方法,您可以在其中输入要追溯的当前月份和月份数量的参数,然后将所有这些转换为天数.例如,如果当前月份为1月,而我想将过去5个月转换为几天,则答案为153.我有以下信息供使用.

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

}

使用calendar.add(Calendar.MONTH, -1 );每月回来.

use calendar.add(Calendar.MONTH, -1); for Month back.

使用calendar.add(Calendar.MONTH, 1 );持续一个月.

use calendar.add(Calendar.MONTH, 1); for month forward.

这篇关于使用Java将几个月的时间转换成几天的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆