我正在尝试创建一个方法,您可以输入您想要追溯的当前月份和月份的参数,并将所有这些转换为天数。例如,如果当前月份是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;
}
答案 0 :(得分:2)
为什么在所有东西都是Java时制作自己的算法。
只需计算现在和之前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));
希望它有所帮助。
问候。
答案 1 :(得分:2)
将此代码用于您想要的内容
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 );一个月前进。
答案 2 :(得分:0)
此实施的主要问题是它固定为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;
}
答案 3 :(得分:0)
我不确定你是否被允许使用SimpleDateFormat但是这里有一个非常简单的IMO示例。不知道社区对此的看法,但它有效:
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;
}
}
此处没有检查您是否传递了有效参数,也没有考虑回溯一年以上。
使用此方法,您可以传递所需月份的数量和返回的月数,它将为您提供天数差异。