gpt4 book ai didi

java - 使用 Java 将一组月份转换为几天

转载 作者:行者123 更新时间:2023-11-29 03:47:52 25 4
gpt4 key购买 nike

我正在尝试制作一种方法,您可以在其中输入参数、当前月份和要追溯的月份数量,并将所有这些转换为天数。例如,如果当前月份是 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;
}

最佳答案

将此代码用于您想要的

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);一个月前。

使用 calendar.add(Calendar.MONTH, 1); future 一个月。

关于java - 使用 Java 将一组月份转换为几天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com